C#.Net 4.5 PropertyGrid:如何隐藏属性

问题很简单(我希望这有一个简单的解决方案!):我想隐藏(Browsable(false))属性“Element”(在我的PropertyGrid对象中),当它为零时。

public class Question { ... public int Element { get; set; } } 

你可以做的是重新使用我在这个问题的答案中描述的DynamicTypeDescriptor类SO: PropertyGrid Browsable找不到entity framework创建的属性,如何找到它?

像这样例如:

 public Form1() { InitializeComponent(); DynamicTypeDescriptor dt = new DynamicTypeDescriptor(typeof(Question)); Question q = new Question(); // initialize question the way you want if (q.Element == 0) { dt.RemoveProperty("Element"); } propertyGrid1.SelectedObject = dt.FromComponent(q); } 

在PropertGrid和Custom Control中为我隐藏属性的最简单方法是:

 public class Question { ... [Browsable(false)] public int Element { get; set; } } 

尝试BrowsableAttributes / BrowsableProperties和HiddenAttributes / HiddenProperties:

更多信息在这里