To describe Clemens's answer in detail above, here is a simple way to use these events (in the collection and on elements) to implement the IsDirty flag, as you described:
public class DirtyCollection<T> : ObservableCollection<T> where T : INotifyPropertyChanged { private bool isDirty = false; public bool IsDirty { get { return this.isDirty; } } public void Clean() { this.isDirty = false; } protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
The code should be clear enough.
You can, of course, delete "where T: INotifyPropertyChanged" so that objects that do not implement this interface should be stored in the collection, but then you will not be notified of changes to the properties on them, because without this interface, they cannot notify you of them.
And if you want to track not only that the collection is dirty, but also how, some additions to OnCollectionChanged and OnItemPropertyChanged to record information transmitted in the event could do it beautifully.
source share