c#——Winform PropertyGrid使用(二)

来源:互联网 发布:b站知名UP主黑历史知乎 编辑:程序博客网 时间:2024/05/16 06:06

PropertyGrid 自定义下拉


首先绘制自定义下拉的控件,使用UserControl,当然也可以下拉窗体。

我们这里使用的 是 UserControl


代码如下:

using System.Reflection.Emit;using System.Windows.Forms;using System.Windows.Forms.Design;using Jurassic.AdapterConfigCommon.Dialogs;using Microsoft.Practices.CompositeUI.Utility;namespace Jurassic.AdapterConfig.FileEntityRetrieveModule.Views.EntityRetrieveView{    public partial class DataSourceControl : UserControl    {        public DataSourceControl()        {            InitializeComponent();        }        private void tvDataSource_AfterSelect(object sender, TreeViewEventArgs e)        {            this.Text = e.Node.Text;            //选择之后如何关闭下拉???        }        private void llbAddSource_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)        {            var frm = new DataSourceSetView();            if (frm.ShowDialog() == DialogResult.OK)            {                MessageBox.Show(@"数据源添加成功!");                this.tvDataSource.Nodes.Add(new TreeNode(frm.DataSourceName));            }        }        private void tvDataSource_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)        {            var selectText = e.Node.Text;            this.txbDescription.Text = @"当前绑定的数据源是:" + selectText;        }    }}
其中最重要的就是要给当前自定义的UserControl 的text 赋值。然后将text值传递给PropertyGrid的属性值

然后就是扩展UITypeEditor, 【     提供可用于设计值编辑器的基类,这些编辑器可提供用户界面 (UI),用来表示和编辑所支持的数据类型的对象值。】

其中就会将上面定义的自定义控件作为值编辑器。

扩展代码如下:

using System.Drawing.Design;using System.Windows.Forms.Design;using Jurassic.AdapterConfig.FileEntityRetrieveModule.Views.EntityRetrieveView;namespace Jurassic.AdapterConfig.FileEntityRetrieveModule.Services.Model.CustomEditor{    public class PropertyGridDataSource : 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)        {            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));            if (edSvc != null)            {                var f = new DataSourceControl();                edSvc.DropDownControl(f);                value = f.Text;            }            return value;        }        public override bool GetPaintValueSupported(System.ComponentModel.ITypeDescriptorContext context)        {            return false;        }    }}
其中最主要的就是
<span style="font-size:18px;">UITypeEditorEditStyle.DropDown;</span>
也就是设置编辑器的显示方式,是下拉的还是模态窗形式的

另外就是,将自定义编辑器的值返回,通过重写方法editValue

至此扩展工作已完成,下面就是具体调用了

首先定义一个propertyGrid绑定的类

代码如下:

using System;using System.ComponentModel;namespace Services.Model{    /// <summary>    /// 实体抓取逻辑 DB 配置信息    /// </summary>    public class DbRetrieveConfig     {        private string _tableName;        private string _fieldName;        private string _sql;        private String _connectionStr = "";        [Editor(typeof(PropertyGridDataSource), typeof(System.Drawing.Design.UITypeEditor)),        Description("获取或设置数据库连接字符串"), Category("实体抓取逻辑(DB Blob)")]        public String 数据库连接        {            get { return _connectionStr; }            set { _connectionStr = value; }        }        [Description("获取或设置表名称"), Category("实体抓取逻辑(DB Blob)")]        public string 表名称        {            get { return _tableName; }            set            {                if (!string.IsNullOrEmpty(value))                {                    _tableName = value.Trim();                }            }        }        [Description("获取或设置表字段"), Category("实体抓取逻辑(DB Blob)")]        public string 表字段        {            get { return _fieldName; }            set            {                if (!string.IsNullOrEmpty(value))                {                    _fieldName = value.Trim();                }            }        }        [Description("获取或设置执行的SQL语句"), Category("实体抓取逻辑(DB Blob)"), EditorAttribute(typeof(PropertyGridRichText),typeof(System.Drawing.Design.UITypeEditor))]        public string SQL        {            get { return _sql; }            set            {                if (!string.IsNullOrEmpty(value))                {                    _sql = value.Trim();                }            }        }    }}

重点是加上属性标注:
Editor(typeof(PropertyGridDataSource), typeof(System.Drawing.Design.UITypeEditor))

这样才可以显示自定的类型


最后绑定到propertyGrid上:

  this.propgrdEntityRetrieve.SelectedObject = new DBConn();

接下来看一下效果吧

1.点击下拉框,显示自定义值编辑器


2.选择树节点之后,值传递给propertygrid的属性值的value







0 0