C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值
关于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控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理