目前,我有一个由PropertyGrid查看的A型对象.然而,其属性之一是B型.属性类型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; } } }
解决方法
为此可以使用
ExpandableObjectConverter
类.
This class adds support for properties
on an object to the methods and
properties provided by TypeConverter.
To make a type of property expandable
in the PropertyGrid,specify this
TypeConverter for standard
implementations of
GetPropertiesSupported and
GetProperties.
要使用此转换器,请使用TypeConverterAttribute
装载有问题的属性,使用typeof(ExpandableObjectConverter)作为构造函数.
public class A { public string Foo { get; set; } public int Bar { get; set; } [TypeConverter(typeof(ExpandableObjectConverter))] public B BooFar { get; set; } }
原文地址:https://www.jb51.cc/csharp/95180.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。