When you add or remove elements from an ObservableCollection
bound to an ItemsSource
, those changes are instantly reflected so what you need to do is simply to modify the ObservableCollection
accordingly as your CRUD operations take place.
Bear in mind that if you are using a version of .NET prior to 4.5 you'll need to dispatch the ObservableCollection
changes to the UI thread.
UPDATE: answering to the comment
What about the following scenario: two datagrids in the view, which are bound to two different ObservableCollections. However, these ObservableCollections are created with a different constructor so as to apply filtering to the underlying Visits. Can you provide a bare bones example as to how I can accomplish the aforementioned?
Filtering is a different story. It is supported by an intermediate layer set between your collection and the items control (ObservableCollection
and DataGrid
respectively in this case): collection views. Actually, you always bind to collection views, not to the collections directly.
So, for your scenario you'd bind both DataGrids
to the same underlying collection but using two different collection views, one for each DataGrid
. This way, the data stays the same and synchronized with whatever you need it to be (your CRUD operations for instance). However, each DataGrid
can display a subset of the data based on filtering, sorted and/or grouped in a different way.
I'd suggest you take a look into the MSDN article regarding data binding to collections.