-1

I have a combobox and its datacontext property is assigned to ObservableCollection.

<ComboBox 
    Name="CB" 
    ItemsSource="{Binding}" 
    DisplayMemberPath="Name" 
    IsSynchronizedWithCurrentItem="True">
</ComboBox>
<TextBlock 
    Name="TB" 
    Text="{Binding ElementName=CB,Path=SelectedItem.Name,UpdateSourceTrigger=PropertyChanged}"
    ></TextBlock>

C#

class Person
{
    public string Name { get; set; }
}

ObservableCollection<Person> people = new ObservableCollection<Person>( new List<Person>()
{
    new Person(){Name="A"},
    new Person(){Name="B"},
    new Person(){Name="C"},
    new Person(){Name="D"},
});

people[0].Name = "Z"; // When button clicked, i execute this

I also have a button when clicked updates the first employee's Name to something else.

When the first employee is the selected one, and I clicked the button even though underlying name changes, it's not reflected in the UI until I change the selected item and re-select the first one.

What should I do to achieve what I want? I thought when ObservableCollection that combobox is bound to changes, it would have been reflected in the ui.

neo
  • 1,952
  • 2
  • 19
  • 39
  • ObservableCollection obviously doesn't have any magic ability to monitor property changes in the items it contains. The items need to implement `INotifyPropertyChanged` and raise the `PropertyChanged` event when their own properties change. – 15ee8f99-57ff-4f92-890c-b56153 Apr 24 '18 at 15:33
  • @EdPlunkett Do you mean the Person class? – neo Apr 24 '18 at 15:34
  • I'm referring to the class contained in the ObservableCollection. The items in the ObservableCollection are instances of some class. That class needs to implement INotifyPropertyChanged. – 15ee8f99-57ff-4f92-890c-b56153 Apr 24 '18 at 15:35
  • 1
    How to implement INotifyPropertyChanged: https://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist – AidanH Apr 24 '18 at 15:37
  • @EdPlunkett So PropertyChanged will indicate to ObservableCollection somethingchanged and in return would trigger CollectionChanged so that combobox would know there is an update, right? – neo Apr 24 '18 at 15:39
  • @neo Not even close. What about objects that aren't contained in an ObservableCollection? Are you suggesting that they can't notify the UI of changes? – 15ee8f99-57ff-4f92-890c-b56153 Apr 24 '18 at 15:40
  • `PropertyChanged` is handled by the `Binding` that binds the viewmodel property to a property of a control in the UI. `CollectionChanged` is unrelated. It's a notification a collection raises when the collection has changed: Add/remove/swap. This is all documented. – 15ee8f99-57ff-4f92-890c-b56153 Apr 24 '18 at 15:42

1 Answers1

0

The underlying object (i.e. Person) of the ObservableCollection needs to implement INotifyPropertyChanged interface.

    class Person : INotifyPropertyChanged
    {
        private string name;
        public string Name 
        { 
          get
          {
             return name;
          } 
          set
          {
              if(value != name)
              {
                  name = value;
                  RaisePropertyChange("Name");
              }
          }
        }

          public event PropertyChangedEventHandler PropertyChanged;

          public void RaisePropertyChange(string propertyname) 
          {  
             if (PropertyChanged != null) 
             {  
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname));  
             }  
          }
    }