Posted by: Zeeshan Amjad | May 18, 2010

Using Composite Collection in C#


Sometimes we need to display data from more than one source in one control and composite collection is exactly for this purpose. With the help of composite collection we can add different data sources and make a composite collection with it. There are already lots of examples of using composite collection using XAML, but here we are going to see the usage of composite collection in C# code.

First we are going to make two collection classes and put some data in it. Here is a code to create two collection classes.

  1: private ObservableCollection<string> cities = new ObservableCollection<string>();
  2: private ObservableCollection<string> states = new ObservableCollection<string>();
  3: 

Now we are going to add some data into these collections. Here is a code to do this.

  1: cities.Add("Frederick");
  2: cities.Add("Germantown");
  3: cities.Add("Arlington");
  4: cities.Add("Burbank");
  5: cities.Add("Newton");
  6: cities.Add("Watertown");
  7: cities.Add("Pasadena");
  8: 
  9: states.Add("Maryland");
 10: states.Add("Virginia");
 11: states.Add("California");
 12: states.Add("Nevada");
 13: states.Add("Ohio");
 14: 

Here is a code to create composite collection in C# using these two collections.

  1: CompositeCollection cmpc = new CompositeCollection();
  2: 
  3: CollectionContainer cc1 = new CollectionContainer();
  4: cc1.Collection = cities;
  5: 
  6: CollectionContainer cc2 = new CollectionContainer();
  7: cc2.Collection = states;
  8: 
  9: cmpc.Add(cc1);
 10: cmpc.Add(cc2);
 11: 

Now we can set the items source property with this composite collection. Here is complete XAML code of the program.

  1: <Window x:Class="WpfCompositeCollection.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:local="clr-namespace:WpfCompositeCollection"
  5:     Title="Composite Collection" Height="300" Width="300">
  6:     <Grid>
  7:         <ListBox Name="list" Margin="5">
  8:         </ListBox>
  9:     </Grid>
 10: </Window>
 11: 

And here is complete C# code of the program.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: using System.Collections.ObjectModel;
 15: 
 16: namespace WpfCompositeCollection
 17: {
 18:     /// <summary>
 19:     /// Interaction logic for Window1.xaml
 20:     /// </summary>
 21:     public partial class Window1 : Window
 22:     {
 23:         private ObservableCollection<string> cities = new ObservableCollection<string>();
 24:         private ObservableCollection<string> states = new ObservableCollection<string>();
 25: 
 26:         public Window1()
 27:         {
 28:             InitializeComponent();
 29: 
 30:             cities.Add("Frederick");
 31:             cities.Add("Germantown");
 32:             cities.Add("Arlington");
 33:             cities.Add("Burbank");
 34:             cities.Add("Newton");
 35:             cities.Add("Watertown");
 36:             cities.Add("Pasadena");
 37: 
 38:             states.Add("Maryland");
 39:             states.Add("Virginia");
 40:             states.Add("California");
 41:             states.Add("Nevada");
 42:             states.Add("Ohio");
 43: 
 44:             CompositeCollection cmpc = new CompositeCollection();
 45: 
 46:             CollectionContainer cc1 = new CollectionContainer();
 47:             cc1.Collection = cities;
 48: 
 49:             CollectionContainer cc2 = new CollectionContainer();
 50:             cc2.Collection = states;
 51: 
 52:             cmpc.Add(cc1);
 53:             cmpc.Add(cc2);
 54: 
 55:             list.ItemsSource = cmpc;
 56:         }
 57:     }
 58: }
 59: 

 

Here is the output of the program.

 

CompositeCollectionOutput

About these ads

Responses

  1. ObservableCollection means that xaml is able to listen to changes?

    • Technically it is not XAML, it is binding (or anyone who is listing those events). When we are using ObservableCollection then it means then all the changes in the collection will be notified and whoever is listing to those events can act accordingly. In case of binding, user control will be updated accordingly. It has nothing to do with XAML. If you do the data binding with code only (C#, VB, VC++.Net, F# etc) without any XAML at all, then it will still update the user interface, if there is any change in the ObservableCollection.

  2. Thanks :) I’m rather in asp.net space where app is responding to requests. I see that it’s a bit different then java’s listeners. Meaning it’s an automatic solution. Thanks for explaining.

    • I have very little experience with Java. In Java i used only JSwing (sometimes AWT) for creating user interface. What i undestand is that writing code in WPF and JSwing is very similar, i.e. create a child control first then put them into panel accordingly. We can do the same thing in WPF with C# code, XAML has an advantage that it is not declerative, but we have to write less code to do the same thing. In addition to this, we don’t have to write test cases to check the UI (because it is declerative). The only thing we have to test is our ViewModel, (there shouldn’t be any logic in Model other than notification and validation, i.e. INotifyPropertyChanged, IDataErrorInfo etc).

      I am not sure what Java listeners is using, but may be the concept is similer with different style of implementation in Java and WPF.

      Regards
      Zeeshan Amjad

  3. Very helpful, thank you!

    • Hi Kdellavalle

      Thanks to like it.

      Regards
      Zeeshan Amjad


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

Categories

Follow

Get every new post delivered to your Inbox.

Join 515 other followers

%d bloggers like this: