首页 > 其他分享 >9.如何把枚举绑定到ComboBox控件上

9.如何把枚举绑定到ComboBox控件上

时间:2024-02-10 09:33:05浏览次数:24  
标签:控件 Description comboBox 枚举 ComboBox new type

1.添加ComboBox控件的枚举绑定扩展类

/// <summary>
    /// ComboBox控件的枚举绑定扩展类
    /// </summary>
    public static class ComboBoxExtension
    {
        /// <summary>
        /// 枚举绑定到ComboBox上
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="comboBox">comboBox</param>
        /// <param name="filter">条件</param>
        /// <param name="icon">图标</param>
        /// <exception cref="Exception"></exception>
        public static void Binding<T>(this ComboBox comboBox, Func<T, bool> filter = null, bool icon = false) where T : struct
        {
            var type = typeof(T);
            if (!type.IsEnum)
            {
                throw new Exception($"{type.FullName}必须为枚举类型");
            }

            var items = type.GetBindableSourceItems<T>(filter);
            comboBox.DoBinding(items, icon);
        }



        public static IEnumerable<BindableSourceItem<T>> GetBindableSourceItems<T>(this object obj, Func<T, bool> filter = null) where T : struct
        {
            var type = typeof(T);
            if (!type.IsEnum)
            {
                throw new Exception($"{type.FullName}必须为枚举类型");
            }

            var items = type.GetEnumValues().Cast<T>().Select(x => new BindableSourceItem<T>()
            {
                Description = x.GetEnumDescription(),
                Value = x,

            });

            if (filter != null)
            {
                items = items.Where(p => filter(p.Value));
            }

            return items;
        }


        public static string GetEnumDescription(this object obj)
        {
            Assert.NotNull(obj);
            var type = obj.GetType();
            if (!type.IsEnum)
            {
                throw new Exception($"{type.FullName}必须为枚举类型");
            }

            FieldInfo fieldInfo = obj.GetType().GetField(obj.ToString());
            if (fieldInfo != null)
            {
                DescriptionAttribute attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attribute != null)
                {
                    return attribute.Description;
                }
                else
                {
                    return fieldInfo.Name;
                }
            }

            return string.Empty;
        }


        public static void DoBinding<T>(this ComboBox comboBox, IEnumerable<BindableSourceItem<T>> items, bool icon = false)
        {
            if (icon)
            {
                FrameworkElementFactory f = new FrameworkElementFactory(typeof(Image));
                System.Windows.Data.Binding binding = new System.Windows.Data.Binding();
                binding.Path = new PropertyPath(nameof(BindableSourceItem<T>.Description));
                f.SetBinding(Image.SourceProperty, binding);
                f.SetValue(FrameworkElement.HeightProperty, 30.0);
                var template = new DataTemplate { VisualTree = f };
                template.Seal();
                comboBox.ItemTemplate = template;
            }
            else
            {
                comboBox.DisplayMemberPath = nameof(BindableSourceItem<T>.Description);
            }

            comboBox.SelectedValuePath = nameof(BindableSourceItem<T>.Value);
            comboBox.ItemsSource = items;
        }
    }

2.添加一个枚举

/// <summary>
    /// 图像缩放模式
    /// </summary>
    public enum ZoomMode
    {
        [Description("自适应")]
        Uniform,
        [Description("100%")]
        Original,
        [Description("200%")]
        Percent200,
        [Description("300%")]
        Percent300,
        [Description("400%")]
        Percent400,
        [Description("500%")]
        Percent500,
        [Description("600%")]
        Percent600,
        [Description("700%")]
        Percent700,
        [Description("800%")]
        Percent800,
    }

3.使用

private void ComboBoxLodeAction(ComboBox obj)
        {
            obj.Binding<ZoomMode>();
        }

 

标签:控件,Description,comboBox,枚举,ComboBox,new,type
From: https://www.cnblogs.com/MingQiu/p/18012745

相关文章

  • 探索C语言中的联合体与枚举:数据多面手的完美组合!
    ​✨✨欢迎大家来到贝蒂大讲堂✨✨......
  • golang之枚举类型iota
    枚举类型是一种常用的数据类型,用于表示一组有限的、预定义的、具名的常量值。在枚举类型中,每个常量都是一个枚举值,它们之间的值相等且唯一。枚举类型通常用于表示一组相关的常量,比如星期、月份、性别等等。在其他语言里(比如Java和C),都内置了枚举类型,而在Go语言里是没有内置......
  • UGUI 基础控件
    基础控件ImageSourceImage:图片来源(图片类型必须是“精灵”类型)Color:图像的颜色Material:图像的材质(一般不修改,会使用UI的默认材质)RaycastTarget:是否作为射线检测的目标(如果不勾选将不会影响射线检测)Maskable:是否能被遮罩ImageType:图片类型Simple:普......
  • RichTextBox控件
    RichTextBox控件用法基本用法基本用法不做赘述富文本用法RTF文档是纯文本文件,可以在不同的软件,和平台之间进行交换和共享,且可以保留文本样式、字体、颜色等格式信息,也可以添加表格。RichTextBox控件中支持RTF格式的文档,可以使流水日志具有各种各样的格式,从而方便查看绘......
  • 【愚公系列】2024年02月 WPF控件专题 Frame控件详解
    ......
  • 关于UniGui 的UniDateTimePicker控件显示问题
    关于UniGui的UniDateTimePicker控件显示问题本人在使用UniGuiUniDateTimePicker控件的显示问题:在UniForm上使用UniDateTimePicker,显示正常。但在Unigrid的列中使用,就出现显示问题,如下图:解决办法:将UniDateTimePicker的属性usesystemformat设置为True,即可显示正常。如下......
  • 洛谷暴力枚举
    暴力枚举一点也不暴力,洛谷题单最后三题还是没做出来,题解也没看懂讲的都是什么搜索七七八八的,看来只能后面学了回来补。个人感觉其实暴力枚举就是要尽可能地压榨运行时间,然后用的也是什么递归搜索for之类的1.统计方形a.如果只是单纯的数格子这种根本不可能做的出来,仔细发现若把......
  • 界面控件DevExpress ASP.NET Spreadsheet组件 - 轻松集成电子表格功能!(一)
    DevExpressASP.NETSpreadsheet组件允许您轻松地将电子表格功能合并到任意ASP.NET应用程序,它可以加载、转换和保存工作簿到XLS-XLSx二进制文件格式,还可以导出和导入XLSX、CSV和TXT文件。P.S:DevExpressASP.NETWebForms Controls拥有针对Web表单(包括报表)的110+种UI控件,可利......
  • 【愚公系列】2024年02月 WPF控件专题 Groupbox控件详解
    ......
  • 微信小程序如何实现动态显示和隐藏某个控件
    Hello大家好!我是咕噜铁蛋!微信小程序作为一种轻量级的应用开发平台,越来越受到开发者和用户的关注。在微信小程序的开发过程中,控制元素的显示和隐藏是一个常见的需求。通过动态显示和隐藏某个控件,我们可以根据用户的操作或特定的条件来提供更好的用户体验。本文铁蛋将为为大家详细介......