0

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.

Cactus
  • 5
  • 3
  • Have you tried the answer in this question? https://stackoverflow.com/questions/2991776/inotifypropertychange-propertychanged-not-firing-when-property-is-a-collection?rq=1 – chadnt Jul 23 '19 at 20:01
  • It’s got fired the first time OnNotifyPropertyChanged (ListA) is called but when I edit ListB, it won’t be fired next time I call OnNotifyPropertyChanged – Cactus Jul 24 '19 at 06:47

1 Answers1

1

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).
Christopher
  • 9,634
  • 2
  • 17
  • 31
  • Using WPF, this is actually not complete. ListView and other components do register themselfs to the OnCollectionChangedEvent of ObservableCollections, however they don't have a mechanism built in to handle replacement of the whole collection. So If you change the collection in the source property, make sure to repair the binding. (Haven't tested it recently, if that behavior changed, please let me know...) – Chillersanim Jul 23 '19 at 22:58
  • @Chillersanim: I rarely use List Views in WPF. usually I bind it to any odd container and let the Data Templates carry me from there. Those prewritten display elements are just too limiting for me. – Christopher Jul 23 '19 at 23:51
  • The OP is probably using those prewritten display elements though, there was no mention of other libraries. Btw [here's](http://updatecontrols.net/doc/tips/common_mistakes_observablecollection.html) more information to the behavior mentioned above. – Chillersanim Jul 24 '19 at 07:50
  • @Chillersanim: it won’t update when I just edit the items of my collection, is observable collection he mentioned helping in this case? I do use ListView btw. – Cactus Jul 24 '19 at 07:59
  • @Cactus: I would need more information to determine the cause. Please provide the code how you bind to the properties in the ListView, what type has and where it is declared. ObservableCollection is usually the way to go if you want data binding on collections. However there might be more problems beside that. – Chillersanim Jul 24 '19 at 08:05
  • @Chillersanim: I've made edits in the post and I hope I didnt miss something – Cactus Jul 24 '19 at 08:34
  • @Cactus: I wrote a somewhat date intro into MVVM, wich is the pattern intended for use with WPF/UWP. https://social.msdn.microsoft.com/Forums/vstudio/en-US/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2/lets-talk-about-mvvm?forum=wpf As was said, the prewritten Display elements all have some issues or a dozen. – Christopher Jul 24 '19 at 09:09