# EnumDataSource

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

* [Overview](https://www.telerik.com/products/wpf/documentation/common-information/consuming-data/enumdatasource#overview)
* [EnumMemberViewModel](https://www.telerik.com/products/wpf/documentation/common-information/consuming-data/enumdatasource#enummemberviewmodel)
* [Setting up a GridViewComboBoxColumn](https://www.telerik.com/products/wpf/documentation/common-information/consuming-data/enumdatasource#setting-up-a-gridviewcomboboxcolumn)

## Overview

The EnumDataSource utility provides an easy way to bind a [RadComboBox](https://www.telerik.com/products/wpf/documentation/controls/radcombobox/getting-started) or a [GridViewComboBoxColumn](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/columntypes/column-types-combobox-column) 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**

```C#
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
}
```

```VB.NET
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<T>** method will return an **IEnumerable** of **EnumMemberViewModel** - one for each [browsable](https://msdn.microsoft.com/en-us/library/system.componentmodel.browsableattribute.browsable) member of the enumeration.

**Example 2: Calling the FromType method**

```C#
	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;
        }
    }
```

```VB.NET
	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**

```XAML
	<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](https://demos.telerik.com/wpf/)

## See Also

* [RadComboBox](https://www.telerik.com/products/wpf/documentation/controls/radcombobox/getting-started)
* [GridViewComboBoxColumn](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/columntypes/column-types-combobox-column)
