示例:WPF中绑定枚举到ComboBox想显示成中文或自定义名称如何实现

一、目的:在开发过程中绑定的枚举不想显示成英文字段怎么办,这里通过TypeConverter的方式来实现绑定的枚举从定义的特性中读取

二、实现

首先定义如下枚举

cs 复制代码
    [TypeConverter(typeof(DisplayEnumConverter))]
    public enum MyEnum
    {
        [Display(Name = "无")]
        None,
        [Display(Name = "第一个")]
        First,
        [Display(Name = "第二个")]
        Second,
        [Display(Name = "第三个")]
        Third
    }

其中DisplayEnumConverter是一个TypeConverter,继承自EnumConverter

cs 复制代码
   public class DisplayEnumConverter : EnumConverter
    {
        public DisplayEnumConverter(Type type)
            : base(type)
        {
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                if (value != null)
                {
                    FieldInfo field = value.GetType().GetField(value.ToString());
                    if (field != null)
                    {
                        DisplayAttribute[] array = (DisplayAttribute[])field.GetCustomAttributes(typeof(DisplayAttribute), inherit: false);
                        if (array.Length == 0 || string.IsNullOrEmpty(array[0].Name))
                        {
                            return value.ToString();
                        }

                        return array[0].Name;
                    }
                }

                return string.Empty;
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            return base.ConvertFrom(context, culture, value);
        }
    }

三、环境

VS2022

四、示例

页面上定义一个ComboxBox并绑定该枚举

XML 复制代码
<ComboBox ItemsSource="{h:GetEnumSource EnumType={x:Type local:MyEnum}}"/>

对GetEnumSource具体实现参考文章:示例:WPF中绑定枚举到ComboBox的方式-CSDN博客

显示效果如下

五、需要了解的知识点

EnumConverter 类 (System.ComponentModel) | Microsoft Learn

XamlType.TypeConverter Property (System.Xaml) | Microsoft Learn

六、源码地址

GitHub - HeBianGu/WPF-ControlDemo: 示例

GitHub - HeBianGu/WPF-ControlBase: Wpf封装的自定义控件资源库

GitHub - HeBianGu/WPF-Control: WPF轻量控件和皮肤库

七、了解更多

System.Windows.Controls 命名空间 | Microsoft Learn

https://github.com/HeBianGu

HeBianGu的个人空间-HeBianGu个人主页-哔哩哔哩视频

相关推荐
samble8 天前
从零搭建半导体设备上位机(九):拖拽与交互,Adorner、DragDrop 与晶圆拖放信息
wpf·拖拽·adorner
郝学胜-神的一滴10 天前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn
波力海苔夹心脆67510 天前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
陈仓主教10 天前
# WPF CheckBox 控件详解与使用—WPF 中的控件(6)--复选框控件(CheckBox)
wpf
光泽雨13 天前
WPF 音视频四大核心方案对比
wpf·音视频
浪扼飞舟13 天前
WPF(数据绑定)
wpf
samble14 天前
从零搭建半导体设备上位机(四):单实例、启动流程与配置文件,App 里那 526 行到底在干什么
wpf·配置文件·单实例
浪扼飞舟15 天前
WPF(控件的继承树、内容控件、布局控件)
wpf
avi911118 天前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpf
android·wpf·框架·数据爬虫·meego·电影管理
吾与谁归in19 天前
C#脚本工具,支持智能提示自动补全(WPF + Roslyn + AvalonEdit)
开发语言·c#·wpf