【发布时间】:2016-09-25 01:11:00
【问题描述】:
我想将 ObservableCollections 的字符串属性绑定到 ComboBox。
型号:
class Sequence : INotifyPropertyChanged
{
public Sequence() { }
private int _id;
public int ID
{
get
{
return _id;
}
set
{
_id = value;
OnPropertyChanged("ID");
}
}
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
视图模型:
private ObservableCollection<Sequence> _storedSequences = new ObservableCollection<Sequence>() { };
public ObservableCollection<Sequence> StoredSequences { get { return _storedSequences; } }
查看 XAML:
<ComboBox x:Name="sequencesComboBox" SelectedIndex="0" ItemsSource="{Binding StoredSequences}" DisplayMemberPath="{Binding Name}" >
问题是 ComboBox 不显示字符串属性。见下图(NQR_GUI_WPF为命名空间):
谁能告诉我我做错了什么?
【问题讨论】:
标签: c# wpf xaml binding combobox