C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值

首先定义一个要在下拉框显示的控件:
using System;
using System.Windows.Forms;
namespace Simon.WinForms.Examples.PropertyGrid
{
    public class EditorControl : UserControl
    {
        public EditorControl()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(21, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "名称:";

            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Items.AddRange(new object[] {
            "名称一",
            "名称二",
            "名称三",
            "名称四"});
            this.comboBox1.Location = new System.Drawing.Point(56, 21);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(133, 20);
            this.comboBox1.TabIndex = 2;
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

            this.SuspendLayout();
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.label1);
            this.Name = "EditorControl";
            this.Size = new System.Drawing.Size(210, 64);
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private Label label1;
        private ComboBox comboBox1;

        public string result = "";
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            result = comboBox1.SelectedItem.ToString();
        }
    }
}
从System.Drawing.Design.UITypeEditor继承一个自定义属性编辑管理器类,参考如下:
using System.Drawing.Design;
using System.Windows.Forms.Design;

namespace Simon.WinForms.Examples.PropertyGrid
{
    public class Editor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            // 编辑属性值时,在右侧显示...更多按钮
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            var edSvc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            if (edSvc != null)
            {
                var popedControl = new EditorControl();
                // 还有ShowDialog这种方式,可以弹出一个窗体来进行编辑
                edSvc.DropDownControl(popedControl);
                value = popedControl.result;
            }
            return base.EditValue(context, provider, value);
        }
    }
}
定义一个使用PropertyGrid显示属性的类型。
using System.ComponentModel;

namespace Simon.WinForms.Examples.PropertyGrid
{
    public class ShowedClass
    {
        [DisplayName("名称")]
        [Editor(typeof(Editor), typeof(System.Drawing.Design.UITypeEditor))]
        public string Name { get; set; }

        [Editor(typeof(Editor), typeof(System.Drawing.Design.UITypeEditor))]
        public string Description { get; set; }
    }
}

在窗体上放好PropertyGrid,然后把你的类实例化后让PropertyGrid来显示设置就可以看到效果了。
propertyGrid1.SelectedObject = new ShowedClass() { Name="我没名字"};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值