Feeds:
Posts
Comments

Posts Tagged ‘Enum’

Well I hope the title of this post is fairly self explanatory. In this example I am going to show you how to first populate a ComboBox with the values in an enumeration. I am then going to databind a property of type enum to the SelectedItem of the ComboBox. If you would like to extend the sample and sort the values displayed in the ComboBox I suggest you check out this post by the almighty Josh Smith. Now on to the code.

First off, here is my animal enumeration:

public enum Animal
{
     Bear,
     Cheetah,
     Deer,
     Elehpant,
}

Here is my MainWindow.cs.

public partial class MainWindow : Window, INotifyPropertyChanged
{
     public Animal MyAnimal
     {
          get { return _myAnimal; }
          set
          {
               if (_myAnimal != value)
               {
                    _myAnimal = value;
                    OnPropertyChanged("MyAnimal");
               }
          }
     }private Animal _myAnimal;

     public MainWindow()
     {
          InitializeComponent();
     }

     #region INotifyPropertyChanged Members

     public event PropertyChangedEventHandler PropertyChanged;

     void OnPropertyChanged(string propName)
     {
          if (this.PropertyChanged != null)
               this.PropertyChanged(
                    this, new PropertyChangedEventArgs(propName));
     }

     #endregion
}

Finally, here is the MainWindow.xaml where I am using an ObjectDataProvider and GetValues to provide the content for my ComboBox’s ItemsSource. When you run the app and change the SelectedItem in the ComboBox, you should see the TextBox update with the newly selected animal. Enjoy!

<Window x:Class="ComboBoxBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ComboBoxBinding"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="125" Width="200"
        x:Name="mainWindow">

     <Window.Resources>
          <ObjectDataProvider x:Key="Animals" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
               <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="local:Animal" />
               </ObjectDataProvider.MethodParameters>
          </ObjectDataProvider>
     </Window.Resources>

     <Grid Background="LightBlue" DataContext="{Binding ElementName=mainWindow}">
          <StackPanel HorizontalAlignment="Center">
               <ComboBox ItemsSource="{Binding Source={StaticResource Animals}}"
                         SelectedItem="{Binding MyAnimal}"
                         Margin="10"/>

               <TextBox Text="{Binding MyAnimal}" Width="100"/>
          </StackPanel>
     </Grid>
</Window>
Advertisements

Read Full Post »

If you have worked with Windows Presentation Foundation (WPF) you should be fully aware of the term data binding. As stated on the MSDN “data binding provides a simple and consistent way for applications to present and interact with data .” It also allows for a clean separation of business logic from the UI and should be used where applicable. A question I commonly see posted on forums is “How do I data bind to an enumeration?”. I hope to clearly address that question here so lets just jump into the code.

First you must create a new WPF project and declare an enumeration. For this example I have created a fruit enum as follows:

public enum Fruit
{
     apples,
     oranges,
     peaches,
     pears,
     strawberries,
}

My MainWindow.cs file will contain only the default constructor and one dependency property of type fruit. One thing to note about the DP is the UIPropertyMetadata. This provides the default value of apples for the DP since that is the first element in the enum. Here is the dependency property:


public Fruit Fruit
{
     get { return (Fruit)GetValue(FruitProperty); }
     set { SetValue(FruitProperty, value); }
}

public static readonly DependencyProperty FruitProperty =
     DependencyProperty.Register("Fruit", typeof(Fruit), typeof(MainWindow), new UIPropertyMetadata(new Fruit()));

Next is the EnumToBooleanConverter which can be seen below. If you are not familiar with binding converters have a look at this.

public class EnumToBooleanConverter : IValueConverter
{
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
          return value.Equals(parameter);
     }

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
          if (value.Equals(false))
               return Binding.DoNothing;
          else
               return parameter;
     }
}

Finally, here is all of the code contained in the MainWindow.xaml file. There are a few things to note here. First, we must include a reference to the local namespace so we can include the binding converter in the Windows.Resources tag. There are also two styles included in the Windows.Resources tag, one for the radio buttons and one for the stack panel. They are referenced using their x:Key.

You can see the binding syntax in the  IsChecked property of each radio button. It is important to note that we are NOT referring to the enums by simply using strings in xaml. We explicitly pass in the enum value using this syntax: x:Static local:Fruit.EnumValue.

<Window x:Class="DatabindingToAnEnumeration.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DatabindingToAnEnumeration"
        Title="MainWindow" Height="250" Width="300"
        x:Name="windowMain">

    <Window.Resources>
        <local:EnumToBooleanConverter x:Key="EnumToBooleanConverter"/>

        <Style x:Key="radioButtonStyle" TargetType="RadioButton">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="Margin" Value="15,0"/>
        </Style>

        <Style x:Key="stackPanelStyle" TargetType="StackPanel">
            <Setter Property="Orientation" Value="Horizontal"/>
            <Setter Property="Background" Value="LightGray"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="15,0"/>
        </Style>

    </Window.Resources>

    <Grid Background="LightYellow" Height="200">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>

        <RadioButton Content="{x:Static local:Fruit.apples}" Grid.Row="0" Style="{StaticResource radioButtonStyle}"
                     IsChecked="{Binding Path=Fruit, ElementName=windowMain, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Fruit.apples}}"/>

        <RadioButton Content="{x:Static local:Fruit.oranges}" Grid.Row="1" Style="{StaticResource radioButtonStyle}"
                     IsChecked="{Binding Path=Fruit, ElementName=windowMain, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Fruit.oranges}}"/>

        <RadioButton Content="{x:Static local:Fruit.peaches}" Grid.Row="2" Style="{StaticResource radioButtonStyle}"
                     IsChecked="{Binding Path=Fruit, ElementName=windowMain, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Fruit.peaches}}"/>

        <RadioButton Content="{x:Static local:Fruit.pears}" Grid.Row="3" Style="{StaticResource radioButtonStyle}"
                     IsChecked="{Binding Path=Fruit, ElementName=windowMain, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Fruit.pears}}"/>

        <RadioButton Content="{x:Static local:Fruit.strawberries}" Grid.Row="4" Style="{StaticResource radioButtonStyle}"
                     IsChecked="{Binding Path=Fruit, ElementName=windowMain, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Fruit.strawberries}}"/>

        <StackPanel Grid.Row="5" Style="{StaticResource stackPanelStyle}">
            <Label Content="Chosen Fruit: " VerticalAlignment="Center"/>
            <TextBlock Text="{Binding Fruit, ElementName=windowMain}" VerticalAlignment="Center" FontWeight="Bold"/>
        </StackPanel>
    </Grid>

</Window>

Just to show that everything is working as expected, I placed a stack panel at the bottom of the window. The panel has a TextBlock that is bound to our Fruit dependency property so you should see it update when you select different radio buttons. Here is an image of the running app.

Hopefully this clears things up. Enjoy!

Read Full Post »