#1- PropertyGrid概述
#2-PropertyGrid基礎(chǔ)應(yīng)用
#3-PropertyGrid綁定自定義類
1新建彈窗Form: EditDialog
1.1新建窗體:EditDialog
最大最小化屬性設(shè)置為False
1.2窗體上放一個(gè)richTextBox:richTextBox1
自定義編輯彈窗
設(shè)置richTextBox1.Modifiers = Public
richTextBox1.Modifiers
1.3修改窗體構(gòu)造函數(shù)EditDialog()
using System.Windows.Forms;
namespace PropertyGrid1
{
public partial class EditDialog : Form
{
public EditDialog(object rtext)
{
InitializeComponent();
richTextBox1.Text = rtext.ToString();
}
}
}
2重載編輯器:UITypeEditor
/// <summary>
/// 重載 編輯器
/// </summary>
class MyTypeEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal; //...彈窗
//return UITypeEditorEditStyle.DropDown; //下拉彈窗
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
EditDialog editForm = new EditDialog(value) { Text = "編輯富文本" };
service.ShowDialog(editForm);
value = editForm.richTextBox1.Text;
return value;
}
}
3自定義類增加自定義編輯器
/// <summary>
/// 自定義彈窗
/// </summary>
public class SysRole5
{
[Description("角色I(xiàn)D"), Category("角色"), DisplayName("角色I(xiàn)D")]
public string role_id { get; set; }
[Description("角色名稱"), Category("角色"), DisplayName("角色名稱")]
public string role_name { get; set; }
[Description("更新日期"), DisplayName("更新日期")]
public DateTime d_time { get; set; }
[Description("注釋"), DisplayName("注釋"),
Editor(typeof(MyTypeEditor), typeof(UITypeEditor))]
public string s_note { get; set; }
}
4綁定帶自定義彈窗的類
private void button6_Click(object sender, EventArgs e)
{
//6自定義彈窗
SysRole5 sr = new SysRole5() { role_id = "9998", role_name = "測(cè)試官",s_note = "富文本編輯彈窗" };
propertyGrid1.SelectedObject = sr;
}
5運(yùn)行效果
自定義彈窗編輯效果
編輯修改完成掺涛,關(guān)閉彈窗庭敦,注釋屬性同步更新。
6擴(kuò)展
彈窗不只可以編輯富文本薪缆,其他任何控件都可以秧廉,如何要傳遞復(fù)雜的信息,自己定義個(gè)類或結(jié)構(gòu)即可拣帽。
7全部代碼
7.1 Form1的全部代碼
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;
namespace PropertyGrid1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
propertyGrid1.SelectedObject = button1;
}
private void button0_Click(object sender, EventArgs e)
{
propertyGrid1.SelectedObject = button0;
}
private void button1_Click(object sender, EventArgs e)
{
//1綁定自定義類
propertyGrid1.SelectedObject = new SysRole();
}
private void button2_Click(object sender, EventArgs e)
{
//2綁定帶分組的類
propertyGrid1.SelectedObject = new SysRole2();
}
private void button3_Click(object sender, EventArgs e)
{
//3綁定中文顯示的類
propertyGrid1.SelectedObject = new SysRole3();
}
private void button4_Click(object sender, EventArgs e)
{
//4綁定帶說明的類
propertyGrid1.SelectedObject = new SysRole4();
}
private void button5_Click(object sender, EventArgs e)
{
//5初始后再綁定
SysRole4 sr = new SysRole4() { role_id = "9998",role_name = "測(cè)試官"};
propertyGrid1.SelectedObject = sr;
}
private void button6_Click(object sender, EventArgs e)
{
//6自定義彈窗
SysRole5 sr = new SysRole5() { role_id = "9998", role_name = "測(cè)試官",s_note = "富文本編輯彈窗" };
propertyGrid1.SelectedObject = sr;
}
}
/// <summary>
/// 系統(tǒng)角色類1
/// </summary>
public class SysRole
{
public string role_id { get; set; }
public string role_name { get; set; }
public DateTime d_time { get; set; }
public string s_note { get; set; }
}
/// <summary>
/// 帶分組的系統(tǒng)角色類
/// </summary>
public class SysRole2
{
[Category("角色")]
public string role_id { get; set; }
[Category("角色")]
public string role_name { get; set; }
public DateTime d_time { get; set; }
public string s_note { get; set; }
}
/// <summary>
/// 中文顯示
/// </summary>
public class SysRole3
{
[Category("角色"), DisplayName("角色I(xiàn)D")]
public string role_id { get; set; }
[ Category("角色"), DisplayName("角色名稱")]
public string role_name { get; set; }
[ DisplayName("更新日期")]
public DateTime d_time { get; set; }
[ DisplayName("注釋")]
public string s_note { get; set; }
}
/// <summary>
/// 帶說明的類
/// </summary>
public class SysRole4
{
[Description("角色I(xiàn)D"), Category("角色"), DisplayName("角色I(xiàn)D")]
public string role_id { get; set; }
[Description("角色名稱"), Category("角色"), DisplayName("角色名稱")]
public string role_name { get; set; }
[Description("更新日期"), DisplayName("更新日期")]
public DateTime d_time { get; set; }
[Description("注釋"), DisplayName("注釋")]
public string s_note { get; set; }
}
/// <summary>
/// 自定義彈窗
/// </summary>
public class SysRole5
{
[Description("角色I(xiàn)D"), Category("角色"), DisplayName("角色I(xiàn)D")]
public string role_id { get; set; }
[Description("角色名稱"), Category("角色"), DisplayName("角色名稱")]
public string role_name { get; set; }
[Description("更新日期"), DisplayName("更新日期")]
public DateTime d_time { get; set; }
[Description("注釋"), DisplayName("注釋"),
Editor(typeof(MyTypeEditor), typeof(UITypeEditor))]
public string s_note { get; set; }
}
/// <summary>
/// 重載 編輯器
/// </summary>
class MyTypeEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal; //...彈窗
//return UITypeEditorEditStyle.DropDown; //下拉彈窗
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
EditDialog editForm = new EditDialog(value) { Text = "編輯富文本" };
service.ShowDialog(editForm);
value = editForm.richTextBox1.Text;
return value;
}
}
}
7.2 EditDialog的全部代碼
using System.Windows.Forms;
namespace PropertyGrid1
{
public partial class EditDialog : Form
{
public EditDialog(object rtext)
{
InitializeComponent();
richTextBox1.Text = rtext.ToString();
}
}
}
7.3 最終運(yùn)行效果
自定義彈窗最終運(yùn)行效果