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

    关于PropertyGrid控件的详细用法请参考文献:

    1、C# PropertyGrid控件应用心得

    2、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="我没名字"};  

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

  • 相关阅读:
    POJ 1045
    POJ 1051
    POJ 1047
    POJ 1050
    POJ 1046
    POJ 1036
    POJ 1035
    POJ 1032
    【洛谷P1412】经营与开发
    【洛谷P3377】【模板】左偏树(可并堆)
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/6158799.html
Copyright © 2011-2022 走看看