您的位置:首页 > 运维架构

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

2013-10-28 14:09 127 查看
第一步:重写UITypeEditor的GetEditStyle方法;

第二部:重写UITypeEditor的EditValue方法;

具体实现如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.Design;
using System.Drawing.Design;
using System.Windows.Forms;

namespace PropertyGridDemo
{
public class PropertyGridRichText:UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}

public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,System.IServiceProvider provider,object value)
{
try
{
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
if (value is string)
{
RichTextBox box = new RichTextBox();
box.Text = value as string;
edSvc.DropDownControl(box);
return box.Text;
}
}
}
catch (Exception ex)
{
System.Console.WriteLine("PropertyGridRichText Error : " + ex.Message);
return value;
}
return value;
}
}
}


调用方式为:

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();
}
}
}


界面实现效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: