Binding Enum to ComboBox
1、添加MarkupExtension
public class EnumToSourceExtension : MarkupExtension { private Type _type; public EnumToSourceExtension(Type type) { _type = type; } public override object ProvideValue(IServiceProvider serviceProvider) { return Enum.GetValues(_type) .Cast<object>() .Select(e => new { Value = (int)e, DisplayName = e.ToString(), Desc = EnumHelper.GetDescription(_type, e) }); ; } }
2、绑定
<ComboBox ItemsSource="{extensions:EnumToSource {x:Type enums:AnalysisType}}" DisplayMemberPath="Desc" SelectedValuePath="Value"> </ComboBox>