Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if an item in my ObservableCollection has changed

Tags:

I have a datagrid which is bound to ObservableCollection<Product>. When the grid is updated this automatically updates the Product object in my collection.

What I want to do now is to have some sort of even that is triggered when any object in the collection is updated -or- some sort of binding to the collection that will return true/false depedant on if any Product has been updated.

The overall objective is to have a save button on my main window that is disabled if no changes have been made to my collection and enabled if changes have been made.

I have read into INotifyPropertyChange but I dont see how I can use this to monitor changes on a whole collection.

Additionally, if I implement this interface on my Product class I dont see how my UI can monitor every product in the collection - or can it?

like image 469
Remotec Avatar asked Jan 24 '12 11:01

Remotec


3 Answers

  • Implement INotifyPropertyChanged in your Product class with notification for every property.
  • Implement INotifyPropertyChanged in your viewmodel.
  • Add property IsDirty to your ViewModel (with notification through INotifyPropertyChanged.
  • In your viewmodel, subscribe to CollectionChanged

    public YourViewModel()
    {
        ...
        YourCollection.CollectionChanged += YourCollection_CollectionChanged; 
        ...
    }
    
    private void YourCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
    {
        if (args.OldItems != null)
            foreach(var oldItem in args.OldItems)
                oldItem.PropertyChanged -= YourItem_PropertyChanged;
    
        if (args.NewItems != null)
            foreach(var newItem in args.NewItems)
                newItem.PropertyChanged += YourItem_PropertyChanged;
    }
    
    private void Youritem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs args)
    {
        IsDirty = true;
    }
    
  • Now you can bind to IsDirty property of your viewmodel, for example, you can bind Button.IsEnabled property directly to it.

like image 146
chopikadze Avatar answered Oct 25 '22 15:10

chopikadze


Just use the ObservableCollection. It has an event called CollectionChanged. If you register it, you can do what you want. Example:

ObservableCollection<string> strings = new ObservableCollection<string>();
strings.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(changed);
strings.Add("Hello");
strings[0] = "HelloHello";

And:

private void changed(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
{
    //You get notified here two times.
}
like image 20
Skalli Avatar answered Oct 25 '22 17:10

Skalli


The logic needs to go in your Model (Product class). A clean approach would be to expose IsDirty property (backed by field) in your model.

And your ViewModel would have a Command binding with CanSave checking the internal collection, and return true if Any of the item in collection IsDirty=true.

like image 31
Manish Basantani Avatar answered Oct 25 '22 15:10

Manish Basantani