I have some properties that could be divided in categories and sub categories. When I add them to the PropertyGrid all subcategories are in the root, but i want sub categories to be children of categories.
class Settings
{
[Category("SubCategory1")]
public bool Property1 { get; set; }
[Category("SubCategory1")]
public bool Property2 { get; set; }
[Category("SubCategory2")]
public bool Property3 { get; set; }
}
PropertyGrid grid = new PropertyGrid();
grid.SelectedObject = new Settings();
I want to make it look like
-Category1
-SubCategory1
Property1
Property2
-SubCategory2
Property3
I have found a solution. The approach is to use TypeConverter Attribute and nested classes.
var settings = new Settings();
settings.Subcategory1 = new SubCategory1(){Property1 = "P1", Property2 = "P2"};
settings.Subcategory2 = new SubCategory2(){Property3="P3"};
grid.SelectedObject = settings;
...
public class Settings
{
[Category("Category1")]
public SubCategory1 Subcategory1 { get; set; }
[Category("Category1")]
public SubCategory2 Subcategory2 { get; set; }
}
[TypeConverter(typeof (ExpandableObjectConverter))]
public class SubCategory1
{
public String Property1 { get; set; }
public String Property2 { get; set; }
public override string ToString()
{
return String.Empty;
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public class SubCategory2
{
public String Property3 { get; set; }
public override string ToString()
{
return String.Empty;
}
}
Hope it will help someone.
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