Show Enum Description Instead of Name

后端 未结 2 1368
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 17:42

I had databinding set up like this:

ItemsSource=\"{Binding Source={my:Enumeration {x:Type credit:OccupationCategory}}         


        
2条回答
  •  感情败类
    2020-12-10 18:06

    This answer is an addition on H.B.'s answer I implemented for my own application:

    Check if the Description attribute is added:

    Description = (value.GetType().GetMember(value.ToString())[0].GetCustomAttributes(true).OfType().Count() > 0 ? 
                                                    value.GetType().GetMember(value.ToString())[0].GetCustomAttributes(true).OfType().First().Description 
                                                    : value)
    

    And Set the following attribute to make sure the correct ID's are used: SelectedValuePath="Value"

提交回复
热议问题