XAML vs C# (Composite Collection) – repost

Well, there are more than one person asked me the same question related to composite collection so I decided to copy the content from my old blog here
Couple days ago, one of the developer in my team came to me and asked for how to use the CompositionCollection in WPF. We can easily use that in XAML, the code will look something likes

   <collectioncontainer Collection="{x:Static local:ApplicationModel.Projects}"/>
   <collectioncontainer Collection="{x:Static local:ClonedModel.Projects}"/>

But we need to support sorting, grouping…it turns out that what we really need here is a CollectionView on top of the composition collection. (We have been using the pattern called Model-View-ViewModel for a long time). So the solution is the use the same construct as the xaml code above but turn them into C#. The code will look something likes below

CompositeCollection cc = new CompositeCollection();
CollectionContainer c1 =
   new CollectionContainer() { Collection = ApplicationModel.Projects };
CollectionContainer c2 =
   new CollectionContainer() { Collection = ClonedModel.Projects };
ICollectionView cv = ((ICollectionViewFactory)cc).CreateView();

As you can see, the syntax between XAML and C# (3.0) in this case is pretty identical, but from here, we can add support for sorting, grouping or filtering


About these ads

One thought on “XAML vs C# (Composite Collection) – repost

  1. For some reason, the XAML code in this blog didn’t come out with the right format. But I think you get the idea

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s