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>

 

posted @ 2018-04-04 10:48  akiing  阅读(162)  评论(0编辑  收藏  举报