PropertyGrid 显示自定义类属性

很多人不清楚PropertyGrid 怎么显示自定义类属性,这里写个小例子供参考



    class A
    {
        string m_Name;
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }

        }

        int m_ID;
        public int ID
        {
            get { return m_ID; }
            set { m_ID = value; }
        }
    }

    class B
    {

        A m_A;
        [TypeConverter(typeof(ExpandableObjectConverter))]   //使用转换器
        public A A
        {
            get { return m_A; }
            set { m_A = value; }
        }

    }


            B b = new B();
            b.A = new A();
            m_PropertyGrid1.SelectedObject = b;
            m_PropertyGrid1.ExpandAllGridItems();
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要创建一个自定义来作为 PropertyGrid 的对象。这个可以包含多个属性以及子属性。例如: ```c# public class Person { public string Name { get; set; } public int Age { get; set; } public Address Address { get; set; } } public class Address { public string Street { get; set; } public string City { get; set; } public string State { get; set; } } ``` 接下来,我们需要创建一个自定义PropertyDescriptor ,用于控制 PropertyGrid显示方式。这个需要继承自 PropertyDescriptor ,并重写几个方法,包括 GetValue()、SetValue()、CanResetValue()、ResetValue()、ShouldSerializeValue() 等方法。例如: ```c# public class NestedPropertyDescriptor : PropertyDescriptor { private PropertyDescriptor _parent; private PropertyDescriptor _child; public NestedPropertyDescriptor(PropertyDescriptor parent, PropertyDescriptor child) : base(child.Name, null) { _parent = parent; _child = child; } public override object GetValue(object component) { object parentValue = _parent.GetValue(component); if (parentValue == null) return null; return _child.GetValue(parentValue); } public override void SetValue(object component, object value) { object parentValue = _parent.GetValue(component); if (parentValue == null) return; _child.SetValue(parentValue, value); OnValueChanged(component, EventArgs.Empty); } public override bool CanResetValue(object component) { object parentValue = _parent.GetValue(component); if (parentValue == null) return false; return _child.CanResetValue(parentValue); } public override void ResetValue(object component) { object parentValue = _parent.GetValue(component); if (parentValue == null) return; _child.ResetValue(parentValue); OnValueChanged(component, EventArgs.Empty); } public override bool ShouldSerializeValue(object component) { object parentValue = _parent.GetValue(component); if (parentValue == null) return false; return _child.ShouldSerializeValue(parentValue); } public override Type ComponentType { get { return _parent.ComponentType; } } public override bool IsReadOnly { get { return _child.IsReadOnly; } } public override Type PropertyType { get { return _child.PropertyType; } } public override string DisplayName { get { return _child.DisplayName; } } } ``` 最后,我们需要创建一个自定义的 TypeConverter ,用于控制 PropertyGrid显示方式。这个需要继承自 ExpandableObjectConverter ,并重写几个方法,包括 GetProperties()、GetPropertiesSupported() 等方法。例如: ```c# public class NestedTypeConverter : ExpandableObjectConverter { public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) { PropertyDescriptorCollection properties = base.GetProperties(context, value, attributes); List<PropertyDescriptor> nestedProperties = new List<PropertyDescriptor>(); foreach (PropertyDescriptor property in properties) { if (property.PropertyType.IsClass && property.PropertyType != typeof(string)) { PropertyDescriptorCollection nestedProps = TypeDescriptor.GetProperties(property.PropertyType, new Attribute[] { new BrowsableAttribute(true) }); foreach (PropertyDescriptor nestedProp in nestedProps) { nestedProperties.Add(new NestedPropertyDescriptor(property, nestedProp)); } } else { nestedProperties.Add(property); } } return new PropertyDescriptorCollection(nestedProperties.ToArray()); } public override bool GetPropertiesSupported(ITypeDescriptorContext context) { return true; } } ``` 最后,我们可以在窗体中使用 PropertyGrid 控件来显示我们自定义。例如: ```c# private void Form1_Load(object sender, EventArgs e) { PropertyGrid1.SelectedObject = new Person { Name = "John", Age = 30, Address = new Address { Street = "123 Main St", City = "Anytown", State = "CA" } }; PropertyGrid1.BrowsableAttributes = new Attribute[] { new BrowsableAttribute(true) }; PropertyGrid1.ExpandAllGridItems(); } ``` 以上就是 C# PropertyGrid 自定义多层显示的示例。希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值