Cactus
Cactus

Reputation: 5

INotifyPropertyChanged won't update a property if only the properties' elements changed

I'm using a generic list as my property. I am calling INotifyPropertyChange to update this property to UI but it won't work unless the whole list has changed.

The code that is working:

public class ClassA : INotifyPropertyChange
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnNotifyPropertyChanged( string p)
    {
        if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(p));
    }

   public List<ClassB> listA
   {
       get
       {
           return new list<ClassB>()
           {
              new ClassB(property1, property2),
           };
        }
}

The code that is not working:

public class ClassA : INotifyPropertyChange
{
    public list<ClassB> listB = new list<ClassB>(new ClassB(property1, property2) );

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnNotifyPropertyChanged( string p)
    {
        if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(p));
     }

    public List<ClassB> listA
    {
       get
       {
           foreach(item a in ListB)
           { a.property1 = something, a.property2 = something}
           return listB;//aparently, items in ListB have changed but UI doesnt update
       }
    }
}

For the ClassB I have:

public class ClassB
{
    public string Property1 {get;set;}
    public string Property2 {get;set;}
}

I will call OnNotifyPropertyChanged("ListA") every second in an Update() function.

InXAML,

 <ListBox Style="{StaticResource ListBoxListAStyle}" Grid.Row="2" ItemsSource="{Binding ListA }" /> 

As described, the first one is working fine but when I make a change on ListB and give back ListB to ListA, it will not update the UI anymore. PS: I called OnNotifyPropertyChanged(string ListA) somewhere else and I think this should be no problem.

EDIT: In the second case, I'm not sure how to check if my PropertyChanged has been fired or not when I call OnNotifyPropertyChanged(string ListA). But it just won't update the UI. I tried to use ObservableCollection <T> instead of List<T>, but seemingly It's not that simple to just change List to ObservableCollection? One strange thing I notice is in the ListBox of the corresponding XAML, ItemsSource for ListA is changing even when the program's paused with a breaking point. I can see it's changing when I open/close XAML file but it won't update UI for me.

UPDATE: Seemingly, it starts working once you have implemented INotifyPropertyChange interface to ClassB in this case. Hope this could have helped you in any way.

Upvotes: 0

Views: 132

Answers (1)

Christopher
Christopher

Reputation: 9804

With collections, there is 3 kinds of Change Notification you need:

  1. The Change Notification on each Property of ClassA
  2. The Change Notification on the Collection[ClassA] you got, so any additions and removales are communicated. That one ObservableCollection can take care off for you.
  3. The Change Notification on the Property on wich ObservableCollection[ClassA] is exposed. For cases when you need to repalce the whole Collection. Wich you will have to do with ObservableCollection (it is really bad with bulk changes while exposed).

Upvotes: 1

Related Questions