【C#】在WPF中使用ComboBox绑定枚举类型

在WPF中使用ComboBox绑定枚举类型

一、实例

定义Enum:

cs 复制代码
internal enum StatusEnum
{
    [Description("待处理")]
    Pending,

    [Description("进行中")]
    InProgress,

    [Description("已完成")]
    Completed
}

定义扩展类,获取Description特性的内容:

cs 复制代码
internal static class EnumExtensions
{
    public static string GetDescription(this Enum value)
    {
        var field = value.GetType().GetField(value.ToString());
        var attribute = field?.GetCustomAttribute<DescriptionAttribute>();
        return attribute?.Description ?? value.ToString();
    }
}

值转换

cs 复制代码
internal class EnumToDescriptionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Enum enumValue)
        {
            return enumValue.GetDescription();
        }
        return value.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Enum.Parse(targetType, value.ToString());
    }
}

对象数据源

xml 复制代码
    <UserControl.Resources>
        <ObjectDataProvider x:Key="StatusEnumProvider" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:StatusEnum" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </UserControl.Resources>

ComboBox 绑定数据源

xml 复制代码
 <ComboBox ItemsSource="{Binding Source={StaticResource StatusEnumProvider}}"
       SelectedItem="{Binding SelectedStatus}"
           Width="140" FontSize="14"
           FontFamily="Microsoft Yahei UI"
           Cursor="Hand" >
     <ComboBox.ItemTemplate>
         <DataTemplate>
             <TextBlock Text="{Binding Converter={StaticResource EnumToDescriptionConverter}}"/>
         </DataTemplate>
     </ComboBox.ItemTemplate>
 </ComboBox>

SelectedStatus要在ViewModel里定义

二、ObjectDataProvider

ObjectDataProvider 类在XAML中创建对象并将其用作数据源。

xml 复制代码
  <ObjectDataProvider x:Key="StatusEnumProvider"
  	MethodName="GetValues" 
  	ObjectType="{x:Type sys:Enum}">
      <ObjectDataProvider.MethodParameters>
          <x:Type TypeName="local:StatusEnum" />
      </ObjectDataProvider.MethodParameters>
  </ObjectDataProvider>
  • ObjectType - 数据要返回的对象类型
  • MethodName - 要在 System.Enum类型上运行的方法名称
  • MethodParameters - 要提供给 MethodName 方法的值的集合

等效于以下代码:

cs 复制代码
 var itemsSource= System.Enum.GetValues(typeof(StatusEnum));
相关推荐
寻星探路7 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
lly2024068 小时前
Bootstrap 警告框
开发语言
2601_949146539 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧9 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX9 小时前
服务异步通信
开发语言·后端·微服务·ruby
zmzb01039 小时前
C++课后习题训练记录Day98
开发语言·c++
懒人咖10 小时前
缺料分析时携带用料清单的二开字段
c#·金蝶云星空
猫头虎10 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
YUJIANYUE10 小时前
PHP纹路验证码
开发语言·php
仟濹11 小时前
【Java基础】多态 | 打卡day2
java·开发语言