Posted by: Zeeshan Amjad | October 18, 2009

Binding Enum Data


Binding Enum data is even simpler than binding XML data because here we don’t have to use any XML based technology such as XPath. As we have seen during the binding of XML data that we are using XMlDataProvider class, but in case of Enum we are going to use ObjectDataProvider class. Here is a class diagram of ObjectDataProvider.

DataProvider

Here is XAML code to perform data binding with Enum data type.

  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:     xmlns:objectDataBinding="clr-namespace:objDataProvider"        
  6:     Title="Enum Data Binding" Height="300" Width="400">
  7:     <Window.Resources>
  8:         <ObjectDataProvider x:Key="ObjectData" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
  9:             <ObjectDataProvider.MethodParameters>
 10:                 <x:Type TypeName="objectDataBinding:States"/>
 11:             </ObjectDataProvider.MethodParameters>
 12:         </ObjectDataProvider>
 13:     </Window.Resources>
 14: 
 15:     <ListBox Margin="5" HorizontalContentAlignment="Stretch"
 16:         ItemsSource="{Binding Source={StaticResource ObjectData}}">
 17:     </ListBox>
 18: </Window>
 19: 

Here is a C# code to define the enumerated type.

  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: 
 15: namespace objDataProvider
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         public Window1()
 23:         {
 24:             InitializeComponent();
 25:         }
 26:     }
 27: 
 28:     public enum States
 29:     {
 30:         Maryland,
 31:         California,
 32:         Virginia,
 33:         Georgia,
 34:         Washington
 35:     }
 36: }
 37: 

Here is the output of this program.

EnumDataBinding

The main idea behind this code is GetValues method of enum class. When we call this function then it will return the array that contains all of the enum values and we can call GetName method to print the meaningful name of enumerated type. If we have defined the same structure, then this simple Console based C# code demonstrate this concept and print the name of all enumerated values.

  1: Array array = Enum.GetValues(typeof(States));
  2: 
  3: foreach (int i in array)
  4:     Console.WriteLine(Enum.GetName(typeof(States), i));
  5: 
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: