Back to Devexpress

EnumItemsSourceBehavior Class

wpf-devexpress-dot-mvvm-dot-ui-62ff21b3.md

latest6.6 KB
Original Source

EnumItemsSourceBehavior Class

Binds a control’s ItemsSource property to an enumeration.

Namespace : DevExpress.Mvvm.UI

Assembly : DevExpress.Xpf.Core.v25.2.dll

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
[TargetType(typeof(ListBoxEdit))]
[TargetType(typeof(ItemsControl))]
[TargetType(typeof(LookUpEditBase))]
public class EnumItemsSourceBehavior :
    Behavior<FrameworkElement>
vb
<TargetType(GetType(ItemsControl))>
<TargetType(GetType(ListBoxEdit))>
<TargetType(GetType(LookUpEditBase))>
Public Class EnumItemsSourceBehavior
    Inherits Behavior(Of FrameworkElement)

Remarks

You can use any of the following attributes with enumeration members:

The following code sample assigns the UserRole enumeration to the ComboBoxEdit.ItemsSource property:

xaml
<UserControl ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors">
    <Grid>
        <dxe:ComboBoxEdit>
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:EnumItemsSourceBehavior EnumType="{x:Type common:UserRole}"/>
            </dxmvvm:Interaction.Behaviors>
        </dxe:ComboBoxEdit>
    </Grid>
</UserControl>
csharp
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
}
vb
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
End Enum

Customize Items

The EnumItemsSourceBehavior generates an array of EnumMemberInfo objects based on the specified enumeration and its attributes. This array is assigned to the control’s ItemsSource property.

The following example demonstrates how to use the ComboBoxEdit.ItemTemplate property to customize generated items:

xaml
<dxe:ComboBoxEdit IsTextEditable="False" ApplyItemTemplateToSelectedItem="True">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EnumItemsSourceBehavior EnumType="{x:Type common:UserRole}" 
                                        SortMode="DisplayName" 
                                        ImageSize="20,20"/>
    </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>

View Example: Bind a Control to an Enumeration

The following code snippets (auto-collected from DevExpress Examples) contain references to the EnumItemsSourceBehavior class.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-grid-scrollbar-annotations/CS/WpfApplication25/MainWindow.xaml#L44

xml
<dxmvvm:Interaction.Behaviors>
    <dxmvvm:EnumItemsSourceBehavior EnumType="{x:Type dxg:ScrollBarAnnotationMode}" SortMode="DisplayName"/>
</dxmvvm:Interaction.Behaviors>

wpf-synchronous-theme-preload-with-splashscreen/CS/ThemePreloadwithSplashscreen/ComplexWindow.xaml#L69

xml
<dxmvvm:Interaction.Behaviors>
    <dxmvvm:EnumItemsSourceBehavior EnumType="{x:Type local:PersonPrefix}" ImageSize="16,16" />
</dxmvvm:Interaction.Behaviors>

Inheritance

Object DispatcherObject DependencyObject Freezable Animatable DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase DevExpress.Mvvm.UI.Interactivity.Behavior DevExpress.Mvvm.UI.Interactivity.Behavior<FrameworkElement> EnumItemsSourceBehavior

See Also

EnumItemsSourceBehavior Members

DevExpress.Mvvm.UI Namespace