博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使PropertyGrid控件的属性值可以显示多行的方法
阅读量:6844 次
发布时间:2019-06-26

本文共 2531 字,大约阅读时间需要 8 分钟。

第一步:重写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 PropertyGridDemo11 {12     public class PropertyGridRichText:UITypeEditor13     {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             try22             {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();        }    }}

界面实现效果:

 

转载于:https://www.cnblogs.com/GaoHuhu/p/3392314.html

你可能感兴趣的文章