我有一些属性可以分为类别和子类别。当我将它们添加到PropertyGrid时,所有子类别都在根目录中,但我希望子类别是类别的子类。
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();
我想让它看起来像
-Category1
-SubCategory1
Property1
Property2
-SubCategory2
Property3
答案 0 :(得分:7)
我找到了解决方案。方法是使用TypeConverter属性和嵌套类。
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;
}
}
希望它会对某人有所帮助。