Enum Binding ItemsSource In WPF

通过ObjectDataProvider 获取Enum数据源

public enum TableSelectedType
{
    SelectedOne,
    SelectedTwo,
    SelectedThird
}
<UserControl.Resources>
        <ObjectDataProvider x:Key="odp" MethodName="GetNames" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:TableSelectedType"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
 </UserControl.Resources>
<ComboBox ItemsSource="{Binding Source={StaticResource odp}}"/>

有时候,我们要绑定的是Enum,但想显示它相应的中文字符串。比如“SelectOne”显示为“第一条”。

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
    string[] enmus = value as string[];
    List<string> result=new List<string>();
    foreach (var item in enmus)
    {
        switch (item)
        {
            case "SelectedOne":
               result.Add("第一行");
                break;
            case "SelectedTwo":
                result.Add("第二行");
                break;
            case "SelectedThird":
                result.Add("第三行");
                break;
        }
    }
    return result;
}
<ComboBox ItemsSource="{Binding Source={StaticResource odp}, Converter={StaticResource EnumTypeToStringConverter}}"/>

通过Dictionary来存Enum,并绑定到ItemsSource上

public partial class ConboBoxEnum : UserControl
{
    public ConboBoxEnum()
    {
        InitializeComponent();
        TableSelectedTypeCollection=new Dictionary<TableSelectedType, string>();
        TableSelectedTypeCollection.Add(TableSelectedType.SelectedOne,"第一条");
        TableSelectedTypeCollection.Add(TableSelectedType.SelectedTwo,"第二条");
        TableSelectedTypeCollection.Add(TableSelectedType.SelectedThird,"第三条");
        this.DataContext = this;
    }
    public Dictionary<TableSelectedType, string> TableSelectedTypeCollection { get; set; }
}
<ComboBox ItemsSource="{Binding TableSelectedTypeCollection}" SelectedValue="{Binding TableSelectedType}" DisplayMemberPath="Value"/>

通过特性(Attribute)来获取

public enum TableSelectedType
{
    [Description("选择第一行")]
    SelectedOne,
    [Description("选择第二行")]
    SelectedTwo,
    [Description("选择第三行")]
    SelectedThird
}
public static class EnumHelper
{
   public static T GetEnumAttribute<T>(Enum source)where T:Attribute
   {
       Type type = source.GetType();
       var sourceName = Enum.GetName(type, source);
       FieldInfo field = type.GetField(sourceName);
       object[] attributes = field.GetCustomAttributes(typeof (T), false);
       foreach (var o in attributes)
       {
           if (o is T)
               return (T) o;
       }
       return null;
   }
   public static string GetDescription(Enum source)
   {
       var str = GetEnumAttribute<DescriptionAttribute>(source);
       if (str==null)
           return null;
       return str.Description;
   }
}
public Dictionary<TableSelectedType, string> TableSelectedTypeDictionary { get; set; }
public ConboBoxEnum()
{
    InitializeComponent();
    TableSelectedTypeDictionary=new Dictionary<TableSelectedType, string>();
    TableSelectedTypeDictionary.Add(TableSelectedType.SelectedOne, EnumHelper.GetDescription(TableSelectedType.SelectedOne));
    TableSelectedTypeDictionary.Add(TableSelectedType.SelectedTwo, EnumHelper.GetDescription(TableSelectedType.SelectedTwo));
    TableSelectedTypeDictionary.Add(TableSelectedType.SelectedThird, EnumHelper.GetDescription(TableSelectedType.SelectedThird));
    this.DataContext = this;
}


知识共享许可协议
《Enum Binding ItemsSource In WPF》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     164.25ms