Reputation: 77
I need to enable editing properties of arbitrary objects (the type of object is only known at run-time). I created the following class:
public class Camera
{
[TypeConverter(typeof(ExpandableObjectConverter))]
public object Configuration
{
get
{
return configuration;
}
set
{
configuration = value;
}
}
public Class1 a;
[TypeConverter(typeof(ExpandableObjectConverter))]
public Class1 A
{
get
{
return a;
}
set
{
a = value;
}
}
}
After selecting object "Camera", I can see the property of Class1 on PropertyGrid, but I can't see the property of object "Configuration". How can I fix this problem?
Upvotes: 0
Views: 545
Reputation: 11598
My assumption was that your form becomes visible before the Configuration property was assigned. You didn't supply enough code to see if that was the case. In order to test out my concern, I created two configuration objects:
public class Configuration1
{
public string Test { get; set; }
public byte Test1 { get; set; }
public int Test2 { get; set; }
}
and
public class Configuration2
{
public char Test3 { get; set; }
public List<string> Test4 { get; set; }
}
I modified your camera class to look like this:
public class Camera
{
public Camera()
{
Configuration1 = new Configuration1();
Configuration2 = new Configuration2();
}
private object configuration;
[TypeConverter(typeof(ExpandableObjectConverter))]
public object Configuration { get; set; }
[TypeConverter(typeof(ExpandableObjectConverter))]
public Configuration1 Configuration1 { get; set; }
[TypeConverter(typeof(ExpandableObjectConverter))]
public Configuration2 Configuration2 { get; set; }
}
I then created a form with a PropertyGrid and two Button instances. I configured the form interactions like this:
public partial class Form1 : Form
{
private readonly Camera camera = new Camera();
public Form1()
{
InitializeComponent();
propertyGrid1.SelectedObject = camera;
}
private void Button1Click(object sender, System.EventArgs e)
{
camera.Configuration = new Configuration2();
UpdatePropertyGrid();
}
private void Button2Click(object sender, System.EventArgs e)
{
camera.Configuration = new Configuration1();
UpdatePropertyGrid();
}
private void UpdatePropertyGrid()
{
propertyGrid1.Refresh();
propertyGrid1.ExpandAllGridItems();
}
}
The startup view looks like this:
After clicking the first button:
After clicking the second button:
If you remove the refresh, the property grid does not work correctly. The alternative is to supply an interface with INotifyPropertyChanged on your classes and properties.
Upvotes: 1