繁体   English   中英

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

[英]observable collection not changing combobox

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

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

您对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));
          }
      }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM