[英]observable collection not changing combobox
I have an observable collection which is the item source to a combo box. 我有一个可观察的集合,它是组合框的项目来源。 When I add a new item to the observable collection, it appears in the combobox.
当我将新项目添加到可观察的集合时,它会出现在组合框中。 However, when I update an existing item, it does not update the combo box.
但是,当我更新现有项目时,它不会更新组合框。 What am I doing wrong?
我究竟做错了什么?
xaml: XAML:
<ComboBox ItemsSource="{Binding ChildGroupOC}" DisplayMemberPath="childGroupName" />
observable collection property: 可观察的集合属性:
public ObservableCollection<ChildGroupBO> ChildGroupOC
{
get
{ return childGroupOC;}
set
{
childGroupOC = value;
}
}
public class ChildGroupBO: INotifyPropertyChanged
{
public int parentGroupId { get; set; }
public int childGroupId { get; set; }
public string childGroupName { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
Your implementation of ChildGroupComboBoxBO not only has to implement INotifyPropertyChanged but also call the event on changes: 您对ChildGroupComboBoxBO的实现不仅必须实现INotifyPropertyChanged,而且还要在更改时调用事件:
OnPropertyChanged("parentGroupId");
Example from MSDN : 来自MSDN的示例:
public class Person : INotifyPropertyChanged
{
private string name;
// Declare the event
public event PropertyChangedEventHandler PropertyChanged;
public Person()
{
}
public Person(string value)
{
this.name = value;
}
public string PersonName
{
get { return name; }
set
{
name = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("PersonName");
}
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.