Enumerate is a markup extension and a value converter which allow to bind to Enum
properties in XAML. It also supports other types for which TypeConverter.GetStandardValues()
returns a collection of standard values.
- Providing values of an
Enum
as is. - Providing values of an
Enum
fromDescriptionAttribute
s.
public enum ViewModelStatus
{
[Description("Offen")]
Open,
[Description("Geschlossen")]
Closed,
[Description("In Arbeit")]
InProgress
}
public class ViewModel : INotifyPropertyChanged
{
public ViewModelStatus Status { get; set; }
}
<ComboBox ItemsSource="{e:Enumerate {x:Type local:ViewModelStatus}, Converter={StaticResource EnumToStringConverter}}"
SelectedItem="{Binding Status, Converter={StaticResource EnumToStringConverter}}" />
<!-- ItemsSource will contain Offen, Geschlossen and In Arbeit -->
Specify an ResourceManager
in the Description
property and apply this attribute to an Enum
values.
public class LocalizedDescriptionAttribute : DescriptionAttribute
{
#region Fields
private string resourceName;
#endregion
#region Constructors
public LocalizedDescriptionAttribute(string resourceName)
{
this.resourceName = resourceName;
}
#endregion
#region DescriptionAttribute Members
public override string Description
{
get { return Resources.ResourceManager.GetString(resourceName); }
}
#endregion
#region Properties
public string ResourceName
{
get { return resourceName; }
}
#endregion
}