Ghost1372

everything can be handy

BindToEnum

with this extension you can bind Enum to itemsources

Enum

1
2
3
4
5
6
7
8
9
public enum Status
{
Horrible,
Bad,
SoSo,
Good,
Better,
Best
}
1
<hc:ComboBox ItemsSource="{Binding Source={hc:EnumBindingSource {x:Type local:Status}}}" Margin="10" />

Description

you can create description for enum and bind itemsources to description, you only need to change enum class like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum Status
{
[Description("This is horrible")]
Horrible,
[Description("This is bad")]
Bad,
[Description("This is so so")]
SoSo,
[Description("This is good")]
Good,
[Description("This is better")]
Better,
[Description("This is best")]
Best
}

If you are using a built-in or third party enums and you can not add a Description attribute to it, you can use this solution:

  • Create an string[] Array and create your description in order
1
2
3
4
5
6
<Window.Resources>
<x:Array Type="sys:String" x:Key="myDescription">
<sys:String>Left To Right Description</sys:String>
<sys:String>Right To Left Description</sys:String>
</x:Array>
</Window.Resources>
  • Pass your Description Array to EnumBindingSource
1
<hc:ComboBox ItemsSource="{Binding Source={hc:EnumBindingSource {x:Type FlowDirection}, {StaticResource myDescription}}}" DisplayMemberPath="Value" Margin="10" />

Do not forget that in this case you must use DisplayMemberPath.
If DisplayMemberPath is set to Value, it displays the Description value and if DisplayMemberPath is set to Key, it displays Enum itself.

Localized Description

you can use this attribute for localize enum

1
2
3
4
5
6
7
8
9
10
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum Status
{
[LocalizedDescription("Good", typeof(LangResources))]
Good,
[LocalizedDescription("Better", typeof(LangResources))]
Better,
[LocalizedDescription("Best", typeof(LangResources))]
Best
}
0%