Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Enum to ItemsSource WPF

Tags:

enums

wpf

xaml

How can I set an Enum to a listbox in xaml. However in the listbox I need to display the Description not the name/value of the enum. And then I when I click a button I need the selected enum to be passed in a method via icommand as an enum not a string. For example:

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

Need to bind these enums to a listbox with a displaymemberpath of the description. And then upon a selection in the listbox, pass in the selected enum like so:

  private void ButtonDidClick(MyEnum enum)
  {

  }

XAML:

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

And I know how to do the rest as far as wiring up a command to a button..etc.. Thanks for any help.

like image 603
TMan Avatar asked Dec 21 '22 01:12

TMan


2 Answers

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 static resource:

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

Found this solution here

like image 139
druss Avatar answered Dec 22 '22 14:12

druss


From a production app
Got this off SO a while ago and cannot find the source
Bind the 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 description attribute value. The following can easily be converted to use enum or into an extenstion. I found this implementation is 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;
    }
like image 31
paparazzo Avatar answered Dec 22 '22 15:12

paparazzo