博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值...
阅读量:4654 次
发布时间:2019-06-09

本文共 3635 字,大约阅读时间需要 12 分钟。

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

1、

2、

首先定义一个要在下拉框显示的控件:

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

原文连接:

转载于:https://www.cnblogs.com/rainbow70626/p/6158799.html

你可能感兴趣的文章
JS中window.event事件使用详解
查看>>
ES6深入学习记录(一)class方法相关
查看>>
C语言对mysql数据库的操作
查看>>
INNO SETUP 获得命令行参数
查看>>
HTML5与CSS3权威指南之CSS3学习记录
查看>>
docker安装部署
查看>>
AVL树、splay树(伸展树)和红黑树比较
查看>>
多媒体音量条显示异常跳动
查看>>
运算符及题目(2017.1.8)
查看>>
React接入Sentry.js
查看>>
ssh自动分发密匙脚本样板
查看>>
转 小辉_Ray CORS(跨域资源共享)
查看>>
Linux安装postgresql
查看>>
MyBatis启动:MapperStatement创建
查看>>
【 全干货 】5 分钟带你看懂 Docker !
查看>>
[转]优化Flash性能
查看>>
popStar手机游戏机机对战程序
查看>>
lambda表达式树
查看>>
二次注入原理及防御
查看>>
会话记住已登录功能
查看>>