nikotromus
nikotromus

Reputation: 1064

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:

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

Upvotes: 0

Views: 60

Answers (1)

Chrille
Chrille

Reputation: 1453

Your implementation of ChildGroupComboBoxBO not only has to implement INotifyPropertyChanged but also call the event on changes:

OnPropertyChanged("parentGroupId");

Example from 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));
          }
      }
  }

Upvotes: 1

Related Questions