EnumItemsSourceBehavior

  • 5 minutes to read

The EnumItemsSourceBehavior class allows you to bind an enumeration to the ItemsSource property of any control.

Getting Started with EnumItemsSourceBehavior

Assume that you need to represent the following UserRole enumeration in the ComboBoxEdit control.

public enum UserRole {
    [Image("pack://application:,,,/Images/Admin.png"), Display(Name = "Admin", Description = "High level of access", Order = 1)]
    Administrator,
    [Image("pack://application:,,,/Images/Moderator.png"), Display(Name = "Moderator", Description = "Average level of access", Order = 2)]
    Moderator,
    [Image("pack://application:,,,/Images/User.png"), Display(Name = "User", Description = "Low level of access", Order = 3)]
    User
}

There are several attributes defined for each enum member.

  • Image is the DataAnnotation attribute that allows assigning an image to a corresponding member of the enumeration. Use the AllowImages property to control image visibility.
  • Display is a standard Display attribute that provides a general-purpose attribute that allows you to specify localizable strings for types and members of entity partial classes.

To assign the UserRole enumeration to the LookUpEditBase.ItemsSource property of the ComboBoxEdit control, define the EnumItemsSourceBehavior as follows.

xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
...
<dxe:ComboBoxEdit>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EnumItemsSourceBehavior EnumType="{x:Type common:UserRole}"/>
    </dxmvvm:Interaction.Behaviors>
</dxe:ComboBoxEdit>

As a result, the combo box will be automatically bound to the enum values.

EnumBehDefaultApperance

If you wish, you can further customize the editor's ItemTemplate, for instance:

<dxe:ComboBoxEdit Name="comboBoxEdit" IsTextEditable="False" ApplyItemTemplateToSelectedItem="True">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EnumItemsSourceBehavior EnumType="{x:Type common:UserRole}" SortMode="DisplayName" ImageSize="8,8"/>
    </dxmvvm:Interaction.Behaviors>
    <dxe:ComboBoxEdit.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding Description}" Grid.Row="1" FontSize="9"/>
            </Grid>
        </DataTemplate>
    </dxe:ComboBoxEdit.ItemTemplate>
</dxe:ComboBoxEdit>

EnumBeh-04

Define EnumItemsSourceBehavior from the Visual Studio Designer

SmartTag allows you to use the EnumItemsSourceBehavior from the Visual Studio designer. Below are step-by-step instructions that describe how to do this for the ComboBoxEdit control.

Open the ComboBoxEdit smart tag panel using the SmartTagButton button at the top-right corner of the selected control.

EnumBeh-01

Switch to the MVVM tab and add the EnumItemsSourceBehavior by clicking a corresponding item in the Add Behavior drop down menu. Then, specify the EnumItemsSourceBehavior's properties as shown in the screenshot below.

EnumBeh-02

Build and run the application to see the changes to the combo-box items at runtime.

EnumBeh-03

Example

View Example

Imports System.Collections.ObjectModel
Imports DevExpress.Mvvm.POCO
Imports EnumItemsSourceBehaviorExample.Common

Namespace EnumItemsSourceBehaviorExample.ViewModel
    Public Class MainViewModel
        Protected Sub New()
            Users = New ObservableCollection(Of User)() From {User.Create(0, "Jack", UserRole.Administrator), User.Create(1, "Ron", UserRole.User), User.Create(2, "John", UserRole.User), User.Create(3, "Antoni", UserRole.User), User.Create(4, "Paul", UserRole.Moderator)}
            SelectedRole = UserRole.User
        End Sub
        Public Shared Function Create() As MainViewModel
            Return ViewModelSource.Create(Function() New MainViewModel())
        End Function

        Public Overridable Property Users() As ObservableCollection(Of User)
        Public Overridable Property SelectedRole() As UserRole
    End Class
End Namespace