ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)

前端 未结 18 2689
一生所求
一生所求 2020-11-22 02:06

Does anyone know why this code doesn\'t work:

public class CollectionViewModel : ViewModelBase {  
    public ObservableCollection Con         


        
18条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 02:53

    I try this solution, but only works for me like a RaisePropertyChange("SourceGroupeGridView") when collection changed, that fired for each item add or changed.

    The problem is in:

    public void EntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
         NotifyCollectionChangedEventArgs args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
        OnCollectionChanged(args);
    }
    

    NotifyCollectionChangedAction.Reset this action make a complete rebind of all items in groupedgrid, is equivalent at RaisePropertyChanged. When you use it all groups of gridview refreshed.

    IF you, only want to refresh in UI the group of the new item, you don't use Reset action, you will need simulate a Add action in itemproperty with something like this:

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {         
        var index = this.IndexOf((T)sender);
    
        this.RemoveAt(index);
        this.Insert(index, (T)sender);
    
        var a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, sender);
        OnCollectionChanged(a);
    }
    

    Sorry by my english, and thanks for the base code :), I hope this helps someone ^_^

    Enjoi!!

提交回复
热议问题