欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# PropertyGrid使用案例详解

程序员文章站 2022-03-10 14:45:50
1. 只有public的property能显示出来,可以通过browsableattribute来控制是否显示,通过categoryattribute设置分类,通过descriptionattribu...

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使用案例详解

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使用案例详解的文章就介绍到这了,更多相关c# propertygrid使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: C# PropertyGrid