使PropertyGrid控件的属性值可以显示多行的方法

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


界面实现效果:

 

版权声明:本文为博主原创文章,遵循版权协议,转载请附上原文出处链接和本声明。
本文链接:
欢乐生肖 青海快3 上海时时乐 中华彩票网 欢乐生肖 澳洲幸运8 极速快乐8 99彩票网站 河北快3代理 极速快3