Frage

I want to have a domain model collection class and a view model class which just reflects and projects the elements of the domain model - but is itself read-only.

My thought here is that both types implement the INotifyCollectionChanged interface and the view model type then just acts as a proxy and projector which wrapes the model elements in an element view model type.

The question is: is implementing INotifyCollectionChanged sufficient to enable WPF databinding to e.g. a DataGrid or ListView or what is the minimal set of interfaces needed to enable collection-based databinding?

War es hilfreich?

Lösung

INotifyCollectionChanged won't update your UI if Properties within the elements in your collection change, only if whole elements are added or removed from your collection.

So if you are happy to just track whole element changes, then INotifyCollectionChanged will suffice, any further granularity, and you'll need to implement INotifyPropertyChanged within your property setters.

Another point worth noting, is that if you use an ObservableCollection to house your list, this already implements INotifiyCollectionChanged for you.

Edit:

The following is Microsoft's take;

You can enumerate over any collection that implements the IEnumerable interface. However, to set up dynamic bindings so that insertions or deletions in the collection update the UI automatically, the collection must implement the INotifyCollectionChanged interface. This interface exposes an event that should be raised whenever the underlying collection changes.

WPF provides the ObservableCollection(Of T) class, which is a built-in implementation of a data collection that exposes the INotifyCollectionChanged interface.

Note that to fully support transferring data values from source objects to targets, each object in your collection that supports bindable properties must also implement the INotifyPropertyChanged interface.

Before implementing your own collection, consider using ObservableCollection(Of T) or one of the existing collection classes, such as List(Of T), Collection(Of T), and BindingList(Of T), among many others. If you have an advanced scenario and want to implement your own collection, consider using IList, which provides a non-generic collection of objects that can be individually accessed by index and thus the best performance.

From..

http://msdn.microsoft.com/en-us/library/ms752347.aspx#binding_to_collections

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top