c# propertyGrid下拉选项

实现下面效果的propertygrid属性下拉选择

具体代码如下

//form窗口类

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            properties ps = new properties();
            propertyGrid1.SelectedObject = ps;
        }
    }

 

//属性类

    public class properties
    {
        [Category("性别"),Description("student gender"),
        TypeConverter(typeof(genderItem)) //使用自定义的属性下拉item类
        ]
        public string Gender
        {
            get;
            set;
        }
    }

//自定义属性下拉效果的类,该类主要继承StringConverter类,并重载该类的一些虚拟方法

    public class genderItem:StringConverter
    {
        //true enable,false disable
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(new string[] { "男", "女" }); //编辑下拉框中的items
        }

        //true: disable text editting.    false: enable text editting;
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
    }

//形成checkbox选项样式

属性类的代码如下

        [Category("信息"), Description("student information"),
        Editor(typeof(CheckboxPro),typeof(System.Drawing.Design.UITypeEditor))]
        public bool IsGoodStudent
        {
            get;
            set;
        }

其中checkboxpro类定义如下,用该方法会出了checkbox窗口

    public class CheckboxPro:System.Drawing.Design.UITypeEditor
    {
        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override void PaintValue(System.Drawing.Design.PaintValueEventArgs e)
        {
            ControlPaint.DrawCheckBox(e.Graphics, e.Bounds, ButtonState.Inactive);
        }
    }

posted @   beautifulday  阅读(7344)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示