MVVM / ObservableCollection Question

I have the following XAML:

<Grid x:Name="LayoutRoot"> <sdk:DataGrid AutoGenerateColumns="True" Margin="46,38,0,40" x:Name="FamilyListGrid" HorizontalAlignment="Left" Width="475" ItemsSource="{Binding FamilyList}" SelectedItem="{Binding SelectedFamily, Mode=TwoWay}" /> </Grid> 

The My FamilyList property used in Binding is an ObservableCollection of objects in my view model class. I found that I need to implement INotifyPropertyChanged in the setter of my FamilyList collection or the binding does not work. In my opinion, ObservableCollection has already implemented this. If so, why do I need to implement the notify property?

If that helps, here is my definition of the FamilyList property:

  private ObservableCollection<Services.Family> familyList; public ObservableCollection<Services.Family> FamilyList { get { return familyList; } private set { familyList = value; NotifyPropertyChanged("FamilyList"); } } 
+4
source share
6 answers

ObservableCollection<T> implements INotifyCollectionChanged , which informs the registered event handler about changes in the collection (adding, deleting, sorting elements). However, the DataGrid needs to know if the property of one of your business objects has changed to update the value in the grid. This requires INotifyPropertyChanged .
ObservableCollection<T> also implements INotifyCollectionChanged . However, this can only be used to inform whether a property of the collection has been changed. There is no mechanism that allows a collection to detect whether your business object has been modified (and if it had one, it would register in the INotifyCollectionChanged of your business object :).

+6
source

An ObservableCollection knows how to notify if a collection is changing, that is, an element is added or deleted.

however, if you do the following:

 FamilyList = new ObservableCollection<FamilyList>(); // or FamilyList = GetFamilyList(); 

then you are actually changing the property that your collection contains, and that is different. I guess this is a problem.

+2
source

The ObservableCollection function for INotifyPropertyChanged is mainly used to respond to adding or removing from this collection.

You need to call Notify ... () on the setter because the collection is a property of your ViewModel, and the DataGrid will not respond to changes in your ViewModel properties unless you call Notify ... () when it changes.

Edit: I'm too slow.

+1
source

As long as the property containing the collection was created before the list is bound / set by the DataContext, you should be fine. If the collection will be replaced, as @Phil Sandler says, you need to be notified. If you create, you only do new when declaring a variable or inside the constructor of the class, you will not need to notify the property changed for this property. If you need to clear the list, I would recommend using the Clear collection method rather than replacing it.

+1
source

You are changing an instance of the observed collection, not the contents of the observed collection. Therefore, nothing is reported in the collection.

0
source

You do not need to notify if the collection itself adds or removes items. However, if you change the entire collection using a new / different instance (i.e. familyList = new ObservableCollection<Services.Family>() ), you need to notify about this. If you are really changing the instance, try clearing / refilling the collection.

0
source

Source: https://habr.com/ru/post/1341378/


All Articles