posts - 19,comments - 150,views - 87122

第一步:重写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();
        }
    }
}
复制代码


界面实现效果:

 

posted on   小桥屋檐下  阅读(1924)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示