我正在使用我无法修改的第三方类,我想在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;
答案 0 :(得分: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);