开发者

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>

My FamilyList p开发者_开发技巧roperty used in the Binding is an ObservableCollection of entities in my view model class. I'm finding that I need to implement INotifyPropertyChanged in the setter of my FamilyList collection or the binding doesn't work. It was my understanding that an ObservableCollection already implemented this. If this is the case, why do I need to implement the notify property?

If it helps, here is my FamilyList property definition:

    private ObservableCollection<Services.Family> familyList;
    public ObservableCollection<Services.Family> FamilyList
    {
        get { return familyList; }
        private set 
        { 
            familyList = value;
            NotifyPropertyChanged("FamilyList");
        }
    }


ObservableCollection<T> implements INotifyCollectionChanged which informs a registered event handler about changes in the collection (add,remove,sort of items). However the DataGrid must know if a property of one of your business-objects has changed to update the value in the grid. For this, INotifyPropertyChanged is needed.
ObservableCollection<T> implements also INotifyCollectionChanged. However this can only be used to be informed if a property of the collection has been changed. There is no mechanism that let the collection detect if your business object has been changed (and if it would have, it would register to INotifyCollectionChanged of your business object :).


ObservableCollection knows how to notify if the collection changes i.e. an item is added or removed.

however, if you do the following:

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

then you are actually changing the property that holds your collection, which is different. I'm guessing this is the issue here.


The ObservableCollection's implementation of INotifyPropertyChanged is basically used to react to adding to or removing from that collection.

You need to call Notify...() in the setter, because the collection is a property of your ViewModel and the DataGrid will not react to any changes of your ViewModel's properties, unless you call Notify...() when changed.

Edit: I'm too slow.


As long as the property holding the collection has been created before the list is bound/DataContext set, you should be ok. If the collection is replaced, as @Phil Sandler says, you need to notify. If you create you only perform new when declaring the variable, or inside of the constructor of the class, you should not need notify property changed for that property. If you need to clear the list, I would recommend using the Clear method of the collection, and not replace it.


You are changing the instance of observable collection, not the contents of the observable collection. So the collection has nothing to notify about.


You should not need to notify if the collection itself adds or removes items. However, if you swap the whole collection out with a new/different instance (i.e. familyList = new ObservableCollection<Services.Family>()) you need to notify. If you are indeed changing the instance, consider clearing/repopulating the collection instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜