C# PropertyGrid使用总结

摘要:
1.只能显示公共属性。您可以控制是否通过BrowsebleAttribute显示它们。您可以通过CategoryAttribute设置分类,通过DescriptionAttribute设置描述。属性可以添加到类或属性中。属性上的属性具有更高的优先级;2.enum将由列表框自动表示;3.内置的输入有效性检查,

1. 只有public的property能显示出来,可以通过BrowsableAttribute来控制是否显示,通过CategoryAttribute设置分类,通过DescriptionAttribute设置描述,Attribute可以加在Class上,也可以加在属性上,属性上的Attribute优先级更高;

2. enum会自动使用列表框表示;

3. 自带输入有效性检查,如int类型输入double数值,会弹出提示对话框;

4. 基本类型Array:增加删除只能通过弹出的集合编辑器,修改可以直接展开,值为null时,可以通过集合编辑器创建;

5. 基本类型List:增删改都只能通过集合编辑器,值为null时,能打开集合编辑器,但不能保存结果,所以必须初始化;

6. ReadOnlyAttribute设置为true时,显示为灰色;对List无效,可以打开集合编辑器,在集合编辑器内可以进行增删改;应用于Array时,不能打开集合编辑器,即不能增删,但可以通过展开的方式修改Array元素;

7. 对象:值显示Object.ToString();Class上加了[TypeConverter(typeof(ExpandableObjectConverter))]之后(也可以加在属性上),才能展开编辑,否则显示为灰色只读;不初始化什么也干不了;

8. 基类类型派生类对象:与普通对象没有区别,[TypeConverter(typeof(ExpandableObjectConverter))]加在基类上即可;

9. 对象Array:增加删除只能通过弹出的集合编辑器,修改可以直接展开,值为null时,可以通过集合编辑器创建;

10. 对象List:增加删除修改只能通过弹出的集合编辑器,值为null时,能打开集合编辑器,但不能保存结果,所以必须初始化;

11. Hashtable和Dictionary能打开集合编辑器,但不能编辑;

12. 通过MyDoubleConverter实现Double类型只显示小数点后两位:

    public class MyDoubleConverter : DoubleConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return string.Format("{0:0.00}", value);
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

13. 通过TypeConverter实现用字符串表示对象

    [TypeConverter(typeof(StudentConverter))]
    public class Student
    {
        public Student(string name, int age)
        {
            Name = name;
            Age = age;
        }
        public string Name { get; set; }
        public int Age { get; set; }

        public static Student FromString(string s)
        {
            string name = "Default";
            int age = 0;
            string[] splits = s.Split(',');
            if (splits.Length == 2)
            {
                name = splits[0];
                int.TryParse(splits[1], out age);
            }
            return new Student(name, age);
        }

        public override string ToString()
        {
            return string.Format("{0},{1}", Name, Age);
        }
    }

    public class StudentConverter : TypeConverter
    {
        public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            Student result = null;
            if ((value as string) != null)
            {
                result = Student.FromString(value as string);
            }
            return result;
        }
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return value.ToString();
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

14. 使用UITypeEditor实现文件或目录选择

        // 需要在项目里添加引用:程序集|框架|System.Design
        [Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
        public string FileName { get; set; }

        [Editor(typeof(FolderNameEditor), typeof(UITypeEditor))]
        public string FolderName { get; set; }

15. 使用自定义的下拉式编辑界面

    public class MyAddress
    {
        public string Province { get; set; }
        public string City { get; set; }
        public override string ToString()
        {
            return string.Format("{0}-{1}", Province, City);
        }
    }

    public class MyAddressEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            if (service != null)
            {
                service.DropDownControl(new MyEditorControl(value as MyAddress));
            }
            return value;
        }
    }

    // 实现两个ComboBox用来编辑MyAddress的属性
    public partial class MyEditorControl : UserControl
    {
        private MyAddress _address;
        public MyEditorControl(MyAddress address)
        {
            InitializeComponent();
            _address = address;
            comboBoxProvince.Text = _address.Province;
            comboBoxCity.Text = _address.City;
        }

        private void comboBoxProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            _address.Province = comboBoxProvince.Text;
        }

        private void comboBoxCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            _address.City = comboBoxCity.Text;
        }
    }

    // MyAddress属性声明
    [Editor(typeof(MyAddressEditor), typeof(UITypeEditor))]
    public MyAddress Address { get; set; }

效果如图:

C# PropertyGrid使用总结第1张

16. 实现弹出式编辑对话框,只要将UserControl改成Form,EditStyle改成Modal,service.DropDownControl改成service.ShowDialog

    public partial class MyEditorForm : Form
    {
        private MyAddress _address;
        public MyEditorForm(MyAddress address)
        {
            InitializeComponent();
            _address = address;
            comboBoxProvince.Text = _address.Province;
            comboBoxCity.Text = _address.City;
        }

        private void comboBoxProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            _address.Province = comboBoxProvince.Text;
        }

        private void comboBoxCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            _address.City = comboBoxCity.Text;
        }
    }

    public class MyAddressEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            if (service != null)
            {
                service.ShowDialog(new MyEditorForm(value as MyAddress));
            }
            return value;
        }
    }

17. 密码表示:[PasswordPropertyText(true)]

18. 动态显示/隐藏属性

    class MyData
    {
        public static void SetPropertyAttribute(object obj, string propertyName, Type attrType, string attrField, object value)
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
            Attribute attr = props[propertyName].Attributes[attrType];
            FieldInfo field = attrType.GetField(attrField, BindingFlags.Instance | BindingFlags.NonPublic);
            field.SetValue(attr, value);
        }

        private bool _ShowPassword = false;
        public bool ShowPassword
        {
            get { return _ShowPassword; }
            set
            {
                _ShowPassword = value;
                SetPropertyAttribute(this, "Password", typeof(BrowsableAttribute), "browsable", _ShowPassword);
            }
        }

        [PasswordPropertyText(true)]
        [Browsable(true)]
        public string Password { get; set; }
    }

    public partial class MainFrm : Form
    {
        // 不添加PropertyValueChanged事件,不能实现动态显示/隐藏
        private void myData_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            this.myData.SelectedObject = this.myData.SelectedObject;
        }
    }

19. 提供下拉选项的string

    public class ListStringConverter : StringConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(new string[] { "A", "B" });
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }
    }

    [TypeConverter(typeof(ListStringConverter))]
    public string Name { get; set; }

免责声明:文章转载自《C# PropertyGrid使用总结》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇cmder设置打开时的默认目录mysql存储过程语法及实例下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Jquery easyui教程

目  录 1基本拖放................................................................................................................................................... 4 2构建购物车型拖放..........

list 导出为excel

/// <summary> /// 将一组对象导出成EXCEL /// </summary> /// <typeparam name="T">要导出对象的类型</typeparam> /// <param name="objLis...

Android随笔之——用shell脚本模拟用户按键、触摸操作

  之前写过两篇关于Android中模拟用户操作的博客(其实用一篇是转载的),现在就来讲讲用shell脚本来模拟用户按键操作。本次的目标是用shell脚本打开微信并在其搜索框中搜索相关内容。   本文的模拟功能主要是用adb的input命令来实现,如果你adb的环境变量配置正确的话,在cmd中输入 adb shell input 就可以看见input的用法...

mybatisplus批量插入编辑及sql打印

一、自定义数据方法注入 EasySqlInjector  import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import com.baomidou.my...

wpf mvvm模式下 在ViewModel关闭view

本文只是博主用来记录笔记,误喷 使用到到了MVVM中消息通知功能 第一步:在需要关闭窗体中注册消息   1 public UserView() 2 { 3 this.DataContext = new UserViewModel(); 4 InitializeComponent();...

数据基本类型以及相关举例

数据基本类型: 整型:字节型byte 短整型short  整型int  浮点类型:长整形long  浮点型  fioat   双精度性double  字符型:char  布尔型:boolean   引用类型( reference): 类class   接口interface   数据  array     循环:for   while      do wh...