可观察的集合不改变组合框

时间:2018-02-06 14:54:28

标签: c# wpf xaml

我有一个可观察的集合,它是组合框的项目源。当我向可观察集合添加新项目时,它会出现在组合框中。但是,当我更新现有项目时,它不会更新组合框。我做错了什么?

XAML:

 <ComboBox ItemsSource="{Binding ChildGroupOC}" DisplayMemberPath="childGroupName" />

可观察的集合属性:

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;
}

1 个答案:

答案 0 :(得分:1)

ChildGroupComboBoxBO 的实施不仅需要实施 INotifyPropertyChanged ,还要在更改时调用事件:

OnPropertyChanged("parentGroupId");

来自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));
          }
      }
  }