第一步:重写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 2013-10-28 14:09  小桥屋檐下  阅读(1913)  评论(0编辑  收藏  举报