第一步:重写UITypeEditor的GetEditStyle方法;
第二部:重写UITypeEditor的EditValue方法;
具体实现如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Forms.Design; 7 using System.Drawing.Design; 8 using System.Windows.Forms; 9 10 namespace PropertyGridDemo 11 { 12 public class PropertyGridRichText:UITypeEditor 13 { 14 public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) 15 { 16 return UITypeEditorEditStyle.DropDown; 17 } 18 19 public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,System.IServiceProvider provider,object value) 20 { 21 try 22 { 23 IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); 24 if (edSvc != null) 25 { 26 if (value is string) 27 { 28 RichTextBox box = new RichTextBox(); 29 box.Text = value as string; 30 edSvc.DropDownControl(box); 31 return box.Text; 32 } 33 } 34 } 35 catch (Exception ex) 36 { 37 System.Console.WriteLine("PropertyGridRichText Error : " + ex.Message); 38 return value; 39 } 40 return value; 41 } 42 } 43 }
调用方式为:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PropertyGridDemo { public class Person { public int Id { get; set; } public string Name { get; set; } public string Sex { get; set; } [EditorAttribute(typeof(PropertyGridRichText), typeof(System.Drawing.Design.UITypeEditor)),Description("The person content!")] public string Content { get; set; } } }
界面代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PropertyGridDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); propertyGrid1.SelectedObject = new Person(); } } }
界面实现效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探