示例:WPF中绑定枚举到ComboBox的方式

一、目的:在开发过程中,经常会需要把枚举绑定到ComboxBox下拉列表中,其实方法有很多,这里面通过MarkupExtension扩展GetEnumSourceExtension去绑定到列表


二、实现

定义GetEnumSourceExtension类

    public class GetEnumSourceExtension : System.Windows.Markup.MarkupExtension
    {
        private Type _enumType;

        public Type EnumType
        {
            get { return this._enumType; }
            set
            {
                if (value != this._enumType)
                {
                    if (null != value)
                    {
                        Type enumType = Nullable.GetUnderlyingType(value) ?? value;

                        if (!enumType.IsEnum)
                            throw new ArgumentException("Type must be for an Enum.");
                    }
                    this._enumType = value;
                }
            }
        }

        public GetEnumSourceExtension()
        {

        }

        public GetEnumSourceExtension(Type enumType)
        {
            this.EnumType = enumType;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (null == this._enumType)
                throw new InvalidOperationException("This EnumType must be specified.");
            Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
            Array enumVlues = Enum.GetValues(actualEnumType);

            if (actualEnumType == this._enumType)
                return enumVlues;

            Array tempArray = Array.CreateInstance(actualEnumType, enumVlues.Length + 1);

            enumVlues.CopyTo(tempArray, 1);

            return tempArray;


        }
    }

三、环境


VS2022

四、示例

应用GetEnumSourceExtension扩展绑定到ComboBox数据源

<ComboBox ItemsSource="{h:GetEnumSource EnumType={x:Type HorizontalAlignment}}"/>

 显示效果

五、需要了解的知识点

MarkupExtension 类 (System.Windows.Markup) | 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个人主页-哔哩哔哩视频

相关推荐

  1. wpf ComboBox数据及变更事件

    2024-06-17 23:42:01       53 阅读
  2. WPF绘制矢量图形并界面方法

    2024-06-17 23:42:01       52 阅读
  3. Swift

    2024-06-17 23:42:01       39 阅读
  4. TypeScript

    2024-06-17 23:42:01       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-17 23:42:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-17 23:42:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-17 23:42:01       82 阅读
  4. Python语言-面向对象

    2024-06-17 23:42:01       91 阅读

热门阅读

  1. python 地图+经纬度标记

    2024-06-17 23:42:01       32 阅读
  2. Lua Table(表)

    2024-06-17 23:42:01       26 阅读
  3. 内网穿透的原理:实现远程访问的技术揭秘

    2024-06-17 23:42:01       29 阅读
  4. 佐助题库1228答案

    2024-06-17 23:42:01       30 阅读
  5. Spring Boot 面试热点(二)

    2024-06-17 23:42:01       34 阅读
  6. SQLite 日期 & 时间

    2024-06-17 23:42:01       28 阅读
  7. Linux安装docker

    2024-06-17 23:42:01       28 阅读
  8. xss-lab靶场的level15-level20

    2024-06-17 23:42:01       25 阅读
  9. 知识库的创建(1) - KnowledgeFile文件加载和分割

    2024-06-17 23:42:01       26 阅读