2

我正在使用无法修改的第三方类,我想在 winforms propertygrid 中查看它们。问题是网格不允许我扩展嵌套类。它看起来像这样。

属性网格

第三方代码看起来像这样

[System.Xml.Serialization.XmlRoot("BBB")]
[DataContract]
public class B : INotifyPropertyChanged
{
    [DataMember]
    private Int32 x;
    [DataMember]
    private Int32 y;
    public Int32 X
    {
        get{return x; }
        set{ x = value;}
    }

    public Int32 Y
    {
        get { return y; }
        set { y = value;![enter image description here][1]}
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        RaisePropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    private void RaisePropertyChanged(object sender, PropertyChangedEventArgs arg)
    {
        if (PropertyChanged != null)
            PropertyChanged(sender, arg);
    }

    public override string ToString()
    {
        return string.Format("X {0} , Y {1}", x,y);
    }
}

[System.Xml.Serialization.XmlRoot("AAA")]
[DataContract]
public class A : INotifyPropertyChanged
{
    [DataMember]
    private Int32 i = 18;

    [DataMember]
    private Int32 j = 6;

    [DataMember]
    private B b;

    public Int32 I
    {
        get { return i; }
        set { i = value; }
    }

    public Int32 J
    {
        get { return j; }
        set { j = value; }
    }

    public B BB
    {
        get { return b; }
        set { b = value; }
    }

    public override string ToString()
    {
        return string.Format("I {0} , J {1} B {2}", i, j, b.ToString());
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        RaisePropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    private void RaisePropertyChanged(object sender, PropertyChangedEventArgs arg)
    {
        if (PropertyChanged != null)
            PropertyChanged(sender, arg);
    }
}

我正在像这样初始化propertygrid

A aa = new A();
propertyGrid2.SelectedObject = aa;
4

1 回答 1

0

首先,当您创建基础A项目时,您还必须设置 的值BB,否则它只是 null。

A aa = new A();
aa.BB = new B();
propertyGrid2.SelectedObject = aa;

为了浏览子项目,ExpandableObjectConverter为这些项目添加一个。在您的示例中,B它看起来像这样:

TypeConverterAttribute BAttribute = new TypeConverterAttribute(typeof(ExpandableObjectConverter));
TypeDescriptor.AddAttributes(typeof(B), BAttribute);
于 2016-06-14T13:55:00.350 回答