PropertyGrid实现文件选择项

原来公司的一段代码,现在给朋友写的软件里也用上了,看样用处挺多,所以保存一下。

自定义属性类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
 
namespace MaoYoo.ChinaMobile.Core.PropertyGridUI
{
    public class PropertyGridFileSelector:UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
 
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
 
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
 
            if (edSvc != null)
            {
 
                // 可以打开任何特定的对话框  
                OpenFileDialog dialog = new OpenFileDialog
                {
                    AddExtension = false,
                    Title = "打开文件",
                    Filter = "*.xls|*.xls|*.xlsx|*.xlsx"
                };
                if (dialog.ShowDialog().Equals(DialogResult.OK))
                {
                    return dialog.FileName;
                }
            }
            return value;
        }
    }
}

文件选择定义:

1
2
3
4
5
6
7
8
9
10
[Category("文件")]
[DisplayName("数据文件")]
[Description("用户数据列表文件路径")]
[ReadOnlyAttribute(false), Browsable(true)]
[Editor(typeof(PropertyGridFileSelector), typeof(UITypeEditor))]
public string File
{
    get;
    set;
}

 操作属性的值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
 
namespace MaoYoo.ChinaMobile.Core
{
 
    public class PropertyGridHelper
    {
        /// <summary>
        /// 设置指定PropertyGrid中已设置了SelectObject对象的属性控件中指定某个自定义特性的值
        /// </summary>
        /// <typeparam name="T">要设置的属性类型</typeparam>
        /// <param name="grid">PropertyGrid对象</param>
        /// <param name="name">要设置的属性的名称</param>
        /// <param name="field">要设置的自定义特性名称</param>
        /// <param name="value">要设置的自定义特性的值</param>
        public static void SetPropertyValue<T>(PropertyGrid grid, string name, string field, object value)
        {
            if (grid.SelectedObject == null)
                throw new ArgumentException(string.Format("指定的PropertyGrid对象不包含{0}对象", "SelectObject"));
 
            AttributeCollection attrs = TypeDescriptor.GetProperties(grid.SelectedObject)[name].Attributes;
 
            FieldInfo fld = typeof(T).GetField(field, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
 
            if (fld != null) fld.SetValue(attrs[typeof(T)], value);
 
            grid.Refresh();
        }
        /// <summary>
        /// 设置指对象指定自定义特性或其相关特性的值
        /// </summary>
        /// <typeparam name="T">要设置的属性类型</typeparam>
        /// <param name="graph">要设置的对象</param>
        /// <param name="name">要设置的属性的名称</param>
        /// <param name="field">要设置的自定义特性名称</param>
        /// <param name="value">要设置的自定义特性的值</param>
        public static void SetPropertyValue<T>(object graph, string name, string field, object value)
        {
            if (graph == null)
                throw new ArgumentException(string.Format("指定的对象{0}不能为空", graph));
 
            AttributeCollection attrs = TypeDescriptor.GetProperties(graph)[name].Attributes;
 
            FieldInfo fld = typeof(T).GetField(field, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
 
            fld.SetValue(attrs[typeof(T)], value);
        }
    }
}

 设置某个属性的自定义特性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Category("余额")]
[Description("是否启用余额")]
[DisplayName("余额匹配")]
[ReadOnlyAttribute(false), Browsable(true)]
public bool Enabled
{
    get { return mEnabledBalance; }
    set
    {
        PropertyGridHelper.SetPropertyValue<ReadOnlyAttribute>(this, "Balance", "isReadOnly", !value);
        mEnabledBalance = value;
 
    }
} private bool mEnabledBalance = true;

PropertyGrid实现文件选择项

上一篇:mysql 定时执行


下一篇:[转]SQLSERVER存储过程调用不同数据库的数据_存储过程中通过链接服务器访问远程服务器