The EnumPicker control makes selection of an enumeration value easy. It has full support for enumerated types which have the Flags attribute applied.

To add an EnumPicker to your user interface, simply declare an instance of it, like so:

<dc:EnumPicker EmulationMode="ComboBox" Value="{Binding Attributes, Mode=TwoWay}" />

The EnumPicker’s currently selected value can be set or retrieved through the Value property.

EnumPicker is an ItemsControl. By default, the Items will be populated automatically based on the Type of the enum being displayed. Property EnumType can be used to explicitly set the enum type. If EnumType is left unset and a value for Value property is set, then EnumType will be inferred from that.

Multiple selection is enabled if Flags property is true. By default, Flags is determined automatically from EnumType. If the enum type has the Flags attribute attached, then Flags is set to true. Or, you can explicitly set this property.

The items in the Items collection must be objects of Type EnumPickerValue. Although the default behavior is to create these objects and populate the Items collection automatically, it is possible to add them explicitly in either code or Xaml. By doing so, it is possible to control the display value for each enum value. Here is an example of setting up the Items in Xaml:

<dc:EnumPicker x:Name="MultiSelectRuleFlagSelector" EmulationMode="ListBox" Value="{Binding ElementName=FileSystemTree, Path=MultiSelectRule, Mode=TwoWay}">
    <dc:EnumPickerValue Value="{x:Static dc:AdvTreeMultiSelectRule.AnyNode}" DisplayName="Any Node" />
    <dc:EnumPickerValue Value="{x:Static dc:AdvTreeMultiSelectRule.SameLevel}" DisplayName="Same Level" />
    <dc:EnumPickerValue Value="{x:Static dc:AdvTreeMultiSelectRule.SameParent}" DisplayName="Same Parent" />
    <dc:EnumPickerValue Value="{x:Static dc:AdvTreeMultiSelectRule.SameType}" DisplayName="Same Type" />
    <dc:EnumPickerValue Value="{x:Static dc:AdvTreeMultiSelectRule.SameColumn}" DisplayName="Same Column" />

Note that in the preceding example, integer values could have been used in place of the x:Static markup extensions.

EnumPicker can be easily set up to behave like a ComboBox or like a ListBox by setting the value of EmulationMode appropriately.

Following is the default Style for EnumPicker which can be used as a starting place for creating your own Template or Style.

<Style TargetType="{x:Type dc:EnumPicker}">
        <DataTemplate x:Key="FlagsItemTemplate">
            <CheckBox VerticalAlignment="Center" IsChecked="{Binding IsSelected}" Content="{Binding DisplayName}" />
        <ControlTemplate x:Key="ListBoxTemplate" TargetType="{x:Type dc:EnumPicker}">
                <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" 
                        Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}">
                    <ItemsPresenter Name="ItemsHost" />
    <Setter Property="Padding" Value="5" />
    <Setter Property="Background" Value="White" />
    <Setter Property="BorderBrush" Value="Gray" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="ItemTemplate">
                <RadioButton GroupName="EnumPickerGroup" VerticalAlignment="Center" IsChecked="{Binding IsSelected}" Content="{Binding DisplayName}" />
    <Setter Property="Template">
            <ControlTemplate TargetType="{x:Type dc:EnumPicker}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" />
                    <Grid Margin="{TemplateBinding Padding}">
                            <ColumnDefinition />
                            <ColumnDefinition Width="Auto" />
                        <ContentPresenter Content="{TemplateBinding DisplayValue}"/>
                        <Path Name="Path" Grid.Column="1" Margin="2,0" Visibility="Visible" VerticalAlignment="Center" Fill="Black" Data="M 0,0 L 7,0 L 3.5,4 Z" />
                    <Popup Name="Popup" AllowsTransparency="True" Placement="Bottom" PopupAnimation="Slide" MinWidth="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}"
                                IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsOpen, Mode=TwoWay}">
                        <Border Padding="5" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" BorderThickness="1" CornerRadius="2">
                            <ItemsPresenter Name="ItemsHost" />
        <Trigger Property="Flags" Value="True">
            <Setter Property="ItemTemplate" Value="{StaticResource FlagsItemTemplate}" />
        <Trigger Property="EmulationMode" Value="ListBox">
            <Setter Property="Template" Value="{StaticResource ListBoxTemplate}" />

Related posts:

  1. Brush Control Quick Start Guide
  2. How to set custom Style for Appointment Views
  3. WPF Color Control Quick Start Guide
  4. How to do MVVM with WPF Navigation Pane
  5. Rating Control for WPF Quick Start Guide