c#——Winform PropertyGrid使用(二)

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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值