Posted by: Zeeshan Amjad | October 19, 2009

Binding Enum Data revisited


We discussed how we can bind the enumerated data type, now we are going to extend our study to see how can we do the same thing with built in enumerated values. For built in enumerated values things are even more simpler. We just have to pass the enumerated type, with its namespace, in ObjectDataProvider class. For data binding we even don’t have to write any C# code. Lets take a look at example of DayOfWeek enum defined in System namespace. Here is a complete XAML code to bind DayOfWeek enum with list box.

  1: <Window x:Class="objDataProvider.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:System="clr-namespace:System;assembly=mscorlib"
  5:     Title="Enum Data Binding" Height="300" Width="400">
  6:     <Window.Resources>
  7:         <ObjectDataProvider x:Key="ObjectData" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
  8:             <ObjectDataProvider.MethodParameters>
  9:                 <x:Type TypeName="System:DayOfWeek"/>
 10:             </ObjectDataProvider.MethodParameters>
 11:         </ObjectDataProvider>
 12:     </Window.Resources>
 13: 
 14:     <ListBox Margin="5" HorizontalContentAlignment="Stretch"
 15:         ItemsSource="{Binding Source={StaticResource ObjectData}}">
 16:     </ListBox>    
 17: </Window>
 18: 

Here is the output of this program.

EnumDataBinding_02

If we want to apply the data template then we can also define it very easily. Here is an example of defining data template to bind enumerated type with list box.

  1: <Window x:Class="objDataProvider.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:System="clr-namespace:System;assembly=mscorlib"
  5:     Title="Enum Data Binding" Height="300" Width="400">
  6:     <Window.Resources>
  7:         <ObjectDataProvider x:Key="ObjectData" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
  8:             <ObjectDataProvider.MethodParameters>
  9:                 <x:Type TypeName="System:DayOfWeek"/>
 10:             </ObjectDataProvider.MethodParameters>
 11:         </ObjectDataProvider>
 12:     </Window.Resources>
 13: 
 14:     <ListBox Margin="5" HorizontalContentAlignment="Stretch"
 15:         ItemsSource="{Binding Source={StaticResource ObjectData}}">
 16:         <ListBox.ItemTemplate>
 17:             <DataTemplate>
 18:                 <Border Margin="2" Background="Beige" BorderBrush="Brown" BorderThickness="1" CornerRadius="5">
 19:                     <TextBlock Margin="3" FontSize="16" Text="{Binding}"/>
 20:                 </Border>
 21:             </DataTemplate>
 22:         </ListBox.ItemTemplate>
 23:     </ListBox>    
 24: </Window>
 25: 

Here is the output of this program.

EnumDataBinding_03

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: