4-PropertyGrid绑定自定义弹窗

#1- PropertyGrid概述
#2-PropertyGrid基础应用
#3-PropertyGrid绑定自定义类

1新建弹窗Form: EditDialog

1.1新建窗体:EditDialog

最大最小化属性设置为False

1.2窗体上放一个richTextBox:richTextBox1

自定义编辑弹窗

设置richTextBox1.Modifiers = Public


richTextBox1.Modifiers

1.3修改窗体构造函数EditDialog()

using System.Windows.Forms;

namespace PropertyGrid1
{
    public partial class EditDialog : Form
    {
        public EditDialog(object rtext)
        {
            InitializeComponent();
            richTextBox1.Text = rtext.ToString();
        }
    }
}

2重载编辑器:UITypeEditor

/// 
    /// 重载 编辑器
    /// 
    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自定义类增加自定义编辑器

/// 
    /// 自定义弹窗
    /// 
    public class SysRole5
    {
        [Description("角色ID"), Category("角色"), DisplayName("角色ID")]
        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 = "测试官",s_note = "富文本编辑弹窗" };
            propertyGrid1.SelectedObject = sr;
        }        

5运行效果

自定义弹窗编辑效果

编辑修改完成,关闭弹窗,注释属性同步更新。

6扩展

弹窗不只可以编辑富文本,其他任何控件都可以,如何要传递复杂的信息,自己定义个类或结构即可。

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 = "测试官"};
            propertyGrid1.SelectedObject = sr;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //6自定义弹窗
            SysRole5 sr = new SysRole5() { role_id = "9998", role_name = "测试官",s_note = "富文本编辑弹窗" };
            propertyGrid1.SelectedObject = sr;
        }        
    }

    /// 
    /// 系统角色类1
    /// 
    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; }
    }

    /// 
    /// 带分组的系统角色类
    /// 
    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; }
    }

    /// 
    /// 中文显示
    /// 
    public class SysRole3
    {
        [Category("角色"), DisplayName("角色ID")]
        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; }
    }

    /// 
    /// 带说明的类
    /// 
    public class SysRole4
    {
        [Description("角色ID"), Category("角色"), DisplayName("角色ID")]
        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; }
    }

    /// 
    /// 自定义弹窗
    /// 
    public class SysRole5
    {
        [Description("角色ID"), Category("角色"), DisplayName("角色ID")]
        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; }
    }

    /// 
    /// 重载 编辑器
    /// 
    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 最终运行效果

自定义弹窗最终运行效果

(PropertyGrid教程完)

你可能感兴趣的:(4-PropertyGrid绑定自定义弹窗)