At the moment I have an object of type A which is being viewed by the PropertyGrid. However, one of its properties is of type B. The property which is of type B is not expandable. How can I change this so that:
a) I can expand custom object property's b) Those changes are bound to that property
Here is the code I have so far:
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; }
}
}
To use the property grid, you create a new instance of the PropertyGrid class on a parent control and set SelectedObject to the object to display the properties for. The information displayed in the grid is a snapshot of the properties at the time the object is assigned.
PropertyGrid is a standard component windows form, which is in the first and second version of the . NET Framework. This component allows us to display properties of practically all types of objects, without writing any additional code.
You can use the ExpandableObjectConverter
class for this purpose.
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.
To use this converter, decorate the property in question with the TypeConverterAttribute
, with typeof(ExpandableObjectConverter)
as the constructor-argument.
public class A
{
public string Foo { get; set; }
public int Bar { get; set; }
[TypeConverter(typeof(ExpandableObjectConverter))]
public B BooFar { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With