How to install Enum in ItemsSource WPF

How to set Enum to a list in xaml. However, in the list I need to display a description, not an enumeration name / value. And then I, when I click the button, I need the selected enumeration to be passed in the method via icommand as an enumeration, not a string. For instance:

public enum MyEnum { EnumOne = 0, [Description("Enum One")] EnumTwo = 1, [Description("Enum Two")] EnumTwo = 2, [Description("Enum Three")] } 

You need to associate these enumerations with a list using the displaymemberpath description. Then, after selecting from the list, go to the selected listing as follows:

  private void ButtonDidClick(MyEnum enum) { } 

XAML:

  <ListBox ItemsSource="{Binding MyEnum"} /> ? 

And I know how to do the rest to connect the command to the button ... etc. Thanks for any help.

+4
source share
3 answers

From production application
Got it from SO some time ago and cannot find the source
Bind DisplayMememberPath to value

 public static Dictionary<T, string> EnumToDictionary<T>() where T : struct { Type enumType = typeof(T); // Can't use generic type constraints on value types, // so have to do check like this if (enumType.BaseType != typeof(Enum)) throw new ArgumentException("T must be of type System.Enum"); Dictionary<T, string> enumDL = new Dictionary<T, string>(); foreach (T val in Enum.GetValues(enumType)) { enumDL.Add(val, val.ToString()); } return enumDL; } 

GetDescription Method

For those who want to know how to read the value of a description attribute. The following can be easily converted to using enum or extenstion. I found this implementation to be more flexible.

Using this method, replace val.ToString() with GetDescription(val) .

  /// <summary> /// Returns the value of the 'Description' attribute; otherwise, returns null. /// </summary> public static string GetDescription(object value) { string sResult = null; FieldInfo oFieldInfo = value.GetType().GetField(value.ToString()); if (oFieldInfo != null) { object[] oCustomAttributes = oFieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true); if ((oCustomAttributes != null) && (oCustomAttributes.Length > 0)) { sResult = ((DescriptionAttribute)oCustomAttributes[0]).Description; } } return sResult; } 
+3
source

Use ObjectDataProvider:

 <ObjectDataProvider x:Key="enumValues" MethodName="GetValues" ObjectType="{x:Type System:Enum}"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="local:ExampleEnum"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> 

and then bind to a static resource:

 ItemsSource="{Binding Source={StaticResource enumValues}}" 

Found this solution here

+3
source

You can do this by converting your Enum to a list of MyEnum tuples and using the DisplayMemberPath ListBox parameter to display the description element. When you select a specific Tuple, just take its MyEnum part and use this to set the SelectedEnumValue property to the ViewModel.

Here is the code:

XAML:

 <Window x:Class="EnumToListBox.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <ListBox Grid.Row="0" ItemsSource="{Binding EnumToDescriptions}" SelectedItem="{Binding SelectedEnumToDescription}" DisplayMemberPath="Item2"/> <TextBlock Grid.Row="1" Text="{Binding SelectedEnumToDescription.Item2}"/> </Grid> 

Code behind:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new ViewModel(); } } public class ViewModel : PropertyChangedNotifier { private List<Tuple<MyEnum, string>> _enumToDescriptions = new List<Tuple<MyEnum, string>>(); private Tuple<MyEnum, string> _selectedEnumToDescription; public ViewModel() { Array Values = Enum.GetValues(typeof(MyEnum)); foreach (var Value in Values) { var attributes = Value.GetType().GetField(Value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false); var attribute = attributes[0] as DescriptionAttribute; _enumToDescriptions.Add(new Tuple<MyEnum, string>((MyEnum)Value, (string)attribute.Description)); } } public List<Tuple<MyEnum, string>> EnumToDescriptions { get { return _enumToDescriptions; } set { _enumToDescriptions = value; OnPropertyChanged("EnumToDescriptions"); } } public Tuple<MyEnum, string> SelectedEnumToDescription { get { return _selectedEnumToDescription; } set { _selectedEnumToDescription = value; SelectedEnumValue = _selectedEnumToDescription.Item1; OnPropertyChanged("SelectedEnumToDescription"); } } private MyEnum? _selectedEnumValue; public MyEnum? SelectedEnumValue { get { return _selectedEnumValue; } set { _selectedEnumValue = value; OnPropertyChanged("SelectedEnumValue"); } } } public enum MyEnum { [Description("Item1Description")] Item1, [Description("Item2Description")] Item2, [Description("Item3Description")] Item3, [Description("Item4Description")] Item4 } public class PropertyChangedNotifier : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { var propertyChanged = PropertyChanged; if (propertyChanged != null) { propertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } 
+1
source

Source: https://habr.com/ru/post/1497341/


All Articles