DCSIMG
Collections and WPF collection view types - IHateSpaghetti {code}

IHateSpaghetti {code}

VSX, DSL and Beyond by Eyal Lantzman

Syndication

Coding / Architecture

Extensibility /DSL

Projects

Articles

Collections and WPF collection view types

This is more of a general post on various collections and how does WPF bind to them and what are the interfaces that WPF uses in order to manage those collections.

 

When binding collection to an ItemsControl (TreeView, ListView, ListBox, ComboBox and etc) or to a more simple control (Button, TextBox, TextBlock and etc) WPF creates behind the scene an ICollectionView object.

This object can be one of the following types:

 

But what if you collection implements both IEnumerable and/or IList or/and IBindingList ,well in that case – by preference first of all BindingListCollectionView then ListCollectionView and then CollectionView. However, you can pick CollectionView type instead of some other type use by the following XAML (assuming that you bind to DataContext):

   1: <CollectionViewSource x:Key="myCollection2" CollectionViewType="{x:Type CollectionView}" Source="{Binding Path=.}"/>

If your collection is editable then you can edit it while it's bounded - using IEditableObject and IEditableCollectionView.

 

When binding to ObservableCollection<T> the view type will be ListCollectionView, you can use CollectionViewSource.GetDefaultView([your collection]) in order to retrieve the ICollectionView object and check it's type.

 

In the next post I'll provide a sample on how you can bind the same collection to multiple controls and edit using IEditableObject and IEditableCollectionView, when one of them changes the other will change automatically as well.

Published Monday, March 09, 2009 11:30 PM by Eyal

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: