For your question number 1, the WPF DataGrid has transaction semantics available to you. If the objects in your bound collection implement IEditableObject
, your code will get three notifications: begin edit, end edit, and cancel edit. You can track these notifications and implement a bit of housekeeping code to tell whether or not the DataGrid is being modified. Depending upon your design, you will need to set the DataGrid's SelectionUnit
/SelectionMode
properties to be consistent with the notifications you want.
For your question number 2, when the underlying collection inherits from IList (ObservableCollection
for example), you can get a ListCollectionView
by casting CollectionViewSource.GetDefaultView
.
The ListCollectionView
has several properties on it that will give you the information you need...
CurrentEditItem
andCurrentAddItem
will point to their respective objects when those states are active.IsAddingNew
will tell you if an add transaction is in progressIsEditingItem
will tell you if an edit transaction is in progressIsInUse
will tell if there are any current subscribers
Using these properties and the IEditableObject interface will give you all the information needed to avert the exception you have been getting.