Posted by: Zeeshan Amjad | November 11, 2009

Using ObservableCollection class


ObservableCollection class is inherited by Collection class. Here is the class hierarchy of this class.

ObservableCollection

The main advantage of ObservableCollection over Collection class is that ObservableCollection class implements one additional interface named INotifyCollectionChanged. This interface define only one event named CollectionChange. In this example we are going to study the simple program to see the usage of ObservableCollection without going into the detail of INotifyCollectionChange interface. Here is a XAML code of our program.

  1: <Window x:Class="ObservableCollection.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Observable Collection" Height="300" Width="400">
  5:     <Grid>
  6:         <ListBox Name="list" Margin="5" HorizontalContentAlignment="Stretch">
  7:             <ListBox.ItemTemplate>
  8:                 <DataTemplate>
  9:                     <Border Margin="5" BorderBrush="DarkRed" BorderThickness="2">
 10:                         <StackPanel Margin="5">
 11:                             <TextBlock Foreground="Blue" FontWeight="Bold" Text="{Binding Path=Name}"/>
 12:                             <TextBlock Text="{Binding Path=Capital}"/>
 13:                             <TextBlock Text="{Binding Path=NickName}"/>
 14:                         </StackPanel>
 15:                     </Border>
 16:                 </DataTemplate>
 17:             </ListBox.ItemTemplate>
 18:         </ListBox>        
 19:     </Grid>
 20: </Window>
 21: 

And here is the complete C# code of this 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 ObservableCollection
 17: {
 18:     /// <summary>
 19:     /// Interaction logic for Window1.xaml
 20:     /// </summary>
 21:     public partial class Window1 : Window
 22:     {
 23:         public Window1()
 24:         {
 25:             InitializeComponent();
 26: 
 27:             StateList sl = new StateList();
 28: 
 29:             list.ItemsSource = sl;
 30:         }
 31:     }
 32: 
 33:     public class StateInfo
 34:     {
 35:         public StateInfo(String name, String capital, String nickName)
 36:         {
 37:             Name = name;
 38:             Capital = capital;
 39:             NickName = nickName;
 40:         }
 41: 
 42:         public String Name
 43:         { get; set; }
 44: 
 45:         public String Capital
 46:         { get; set; }
 47: 
 48:         public String NickName
 49:         { get; set; }
 50:     }
 51: 
 52:     public class StateList : ObservableCollection<StateInfo>
 53:     {
 54:         public StateList() //: base()
 55:         {
 56:             Add(new StateInfo("Maryland", "Annapolis", "Old Line State"));
 57:             Add(new StateInfo("Washington", "Olympia", "The Evergreen State"));
 58:             Add(new StateInfo("California", "Sacremento", "Golden State"));
 59:             Add(new StateInfo("Taxes", "Austin", "Lone Star State"));
 60:             Add(new StateInfo("New York", "Albany", "Empire State"));
 61:         }
 62:     }
 63: }
 64: 

Here is the output of this program.

ObservableCollectionOutput

Advertisements

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

%d bloggers like this: