【问题标题】:How do I view object properties in PropertyGrid?如何在 PropertyGrid 中查看对象属性?
【发布时间】:2011-08-11 14:56:59
【问题描述】:

目前我有一个 A 类型的对象,PropertyGrid 正在查看它。但是,它的属性之一是类型 B。类型 B 的属性是不可扩展的。我怎样才能改变这个:

a) 我可以扩展自定义对象属性的 b) 这些更改绑定到该属性

这是我目前的代码:

using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace PropGridTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            A a = new A
            {
                Foo = "WOO HOO!",
                Bar = 10,
                BooFar = new B
                {
                    FooBar = "HOO WOO!",
                    BarFoo = 100
                }
            };

            propertyGrid1.SelectedObject = a;
        }
    }
    public class A
    {
        public string Foo { get; set; }
        public int Bar { get; set; }
        public B BooFar { get; set; }
    }
    public class B
    {
        public string FooBar { get; set; }
        public int BarFoo { get; set; }
    }
}

【问题讨论】:

    标签: c# view propertygrid expand


    【解决方案1】:

    您可以为此目的使用ExpandableObjectConverter 类。

    这个类增加了对属性的支持 在一个对象上的方法和 TypeConverter 提供的属性。 使一种属性可扩展 在 PropertyGrid 中,指定这个 标准的 TypeConverter 的实现 GetPropertiesSupported 和 获取属性。

    要使用此转换器,请使用 TypeConverterAttribute 装饰相关属性,并使用 typeof(ExpandableObjectConverter) 作为构造函数参数。

    public class A
    {
        public string Foo { get; set; }
        public int Bar { get; set; }
    
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public B BooFar { get; set; }
    }
    

    【讨论】:

    • 谢谢!它完全按照我的需要工作。我无法在 8 分钟左右内将您的答案标记为正确……但我会:D
    • 哦,太好了,这正是我需要的,搜索了一个小时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多