New to Telerik UI for WPF? Download free 30-day trial

EnumDataSource

This article will describe and show you how to use the EnumDataSource utility.

Overview

The EnumDataSource utility provides an easy way to bind a RadComboBox or a GridViewComboBoxColumn to an Enum. It will return a collection of viewmodels based on the Enum type supplied. Attributes such as DisplayAttribute and DescriptionAttribute will be read and stored in the viewmodels, so that friendly names can be displayed in the combo-boxes.

For the purposes of this article, we will use the following enumeration:

Example 1: The Position enumeration

public enum Position 
{ 
    [Display(Name = "Goalkeeper", ShortName = "Goalie")] 
    [Description("The player responsible for protecting his team's goal.")] 
    GK = 0, 
    DF = 1, 
    MF = 2, 
    FW = 3 
} 
Public Enum Position 
    <Display(Name := "Goalkeeper", ShortName := "Goalie"), Description("The player responsible for protecting his team's goal.")> 
    GK = 0 
    DF = 1 
    MF = 2 
    FW = 3 
End Enum 

EnumMemberViewModel

Calling the generic FromType method will return an IEnumerable of EnumMemberViewModel - one for each browsable member of the enumeration.

Example 2: Calling the FromType method

IEnumerable<Telerik.Windows.Data.EnumMemberViewModel> positions; 
public IEnumerable<Telerik.Windows.Data.EnumMemberViewModel> Positions 
{ 
    get 
    { 
        if (positions == null) 
        { 
            positions = Telerik.Windows.Data.EnumDataSource.FromType<Position>(); 
        } 
 
        return positions; 
    } 
} 
Private _positions As IEnumerable(Of Telerik.Windows.Data.EnumMemberViewModel) 
Public ReadOnly Property Positions() As IEnumerable(Of Telerik.Windows.Data.EnumMemberViewModel) 
    Get 
        If _positions Is Nothing Then 
            _positions = Telerik.Windows.Data.EnumDataSource.FromType(Of Position)() 
        End If 
 
        Return _positions 
    End Get 
End Property 

The following list describes the public properties the EnumMemberViewModel class provides as well as the evaluated values for the Position.GK member of the enumeration:

  • Description: Returns the Description of the DescriptionAttribute, if present. ("The player responsible for protecting his team's goal.")
  • DisplayShortName: Returns the ShortName of the DisplayAttribute, if present. ("Goalie")
  • Name: Gets the name of the Enum member. ("GK")
  • Value: Gets the actual Enum value. (Position.GK)
  • DisplayName: Returns the first of the following properties that is not null - DisplayShortName, Description, Name. (""Goalie"")

Setting up a GridViewComboBoxColumn

A typical use case would be to bind the DisplayMemberPath of the combo to the viewmodel's DisplayName and SelectedValueMemberPath to be equal to Value, as demonstrated in Example 3.

Example 3: Defining the GridViewComboBoxColumn

<telerik:GridViewComboBoxColumn DataMemberBinding="{Binding Position}" 
                                ItemsSource="{Binding Positions, Source={StaticResource MyViewModel}}" 
                                DisplayMemberPath="DisplayName" 
                                SelectedValueMemberPath="Value" /> 

A practical example of how to use the EnumDataSource utility can be found in the respective demo of the WPF Controls Samples.

See Also

In this article