Binding to enum’s - Rudi Grobler

Binding to enum’s

 

Maximilian asked the following question on Stack Overflow: Databinding an enum property to a ComboBox in WPF?

Let’s create a very simple example

public enum EmployeeType    
{
    Manager,
    Worker
}

Each employee can either be a Manager or a Worker… Here is my Employee “model”

public class Employee : BindableObject
{
    private string name;
    public string Name 
    {
        get { return name; }
        set 
        { 
            name = value;
            RaisePropertyChanged("Name");
        }
    }

    private string surname;       
    public string Surname 
    {
        get { return surname; }
        set
        {
            surname = value;
            RaisePropertyChanged("Surname");
        }
    }

    private EmployeeType type = EmployeeType.Worker;
    public EmployeeType Type 
    {
        get { return type; }
        set 
        { 
            type = value;
            RaisePropertyChanged("Type");
        } 
    }
};

I then create a ObservableCollection<> of dummy data and set my DataContext

DataContext = new ObservableCollection<Employee>() {
    new Employee() { Name = "Butch", Surname = "James", Type = EmployeeType.Worker },
    new Employee() { Name = "John", Surname = "Smith", Type = EmployeeType.Manager },
    new Employee() { Name = "Brian", Surname = "Mujati", Type = EmployeeType.Worker },
    new Employee() { Name = "Tendai", Surname = "Mtawarira", Type = EmployeeType.Worker },
    new Employee() { Name = "BJ", Surname = "Botha", Type = EmployeeType.Worker },  
    new Employee() { Name = "Juan", Surname = "Smit", Type = EmployeeType.Worker }};
Now we are ready to try and create some basic UI for this application!

To bind to the enum, we have 2 choices… we can set the ComboBox’s ItemsSource in the code behind

typeComboBox.ItemsSource = Enum.GetValues(typeof(EmployeeType));

Or we can try and do it the WPF way using only XAML?

<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="sysEnum">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="local:EmployeeType" />
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

Now we have a StaticResource we can bind to… Here is the ComboBox binding

<ComboBox ItemsSource="{Binding Source={StaticResource sysEnum}}" />

I love WPF… Next, lets bind to our dummy data! I created a simple master/detail form

<ListBox ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0,0,0,10">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" Margin="0,0,5,0" />
                    <TextBlock Text="{Binding Surname}" />
                </StackPanel>
                <TextBlock Text="{Binding Type}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Here is my master… it is just a ListBox with all the Employees listed!

<StackPanel Grid.Column="1">
    <StackPanel Orientation="Horizontal">
        <Label Content="Name: " />
        <TextBox Text="{Binding Name}" />
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label Content="Surname: " />
        <TextBox Text="{Binding Surname}" />
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label Content="Type: " />
        <ComboBox ItemsSource="{Binding Source={StaticResource sysEnum}}" SelectedValue="{Binding Type}"/>
    </StackPanel>
</StackPanel>

And finally our details pane where you can change the employee’s name, surname and type!

That is it, if you found this useful, please kick it on DotNetKicks.com

Published Thursday, October 02, 2008 9:36 AM by rudi
Filed under: , ,

Comments

# Binding to enum's

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Thursday, October 02, 2008 9:38 AM by DotNetKicks.com

# Dew Drop - October 2, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - October 2, 2008 | Alvin Ashcraft's Morning Dew

Thursday, October 02, 2008 1:54 PM by Dew Drop - October 2, 2008 | Alvin Ashcraft's Morning Dew

# 2008 October 03 - Links for today &laquo; My (almost) Daily Links

Pingback from  2008 October 03 - Links for today &laquo; My (almost) Daily Links