我需要在项目中使用propertygrid,我需要过滤将要显示的属性。我找到了一种按类别过滤属性网格的方法,但是在过滤它时我需要更深入。
这里是仅显示“外观”类别的代码。但我需要在“外观”下禁用一些属性,如“BackColor”
Attribute myfilterattribute = new CategoryAttribute("Appearance");
pg_1.BrowsableAttributes = new AttributeCollection(new Attribute[] { myfilterattribute });
我如何过滤掉Backcolor?
答案 0 :(得分:1)
如果要静态禁用此道具,例如在编译时,您可以尝试this approach。将它们设置为不可见,运行时间更复杂,因此请查看dynamic property setting。
答案 1 :(得分:1)
我通常使用这种方法在PropertyGrid中显示/隐藏属性(使用Reflection
):
public void ShowValue(string _Who, bool _Enabled)
{
BrowsableAttribute attribute = (BrowsableAttribute)TypeDescriptor.GetProperties(this.GetType())(_Who).Attributes(typeof(BrowsableAttribute));
System.Reflection.FieldInfo fieldToChange = attribute.GetType().GetField("Browsable", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance | Reflection.BindingFlags.Public | Reflection.BindingFlags.Static | Reflection.BindingFlags.IgnoreCase);
fieldToChange.SetValue(attribute, _Enabled);
//Refresh the Property Grid
PG.Refresh();
}
参数是:
我使用的PropertyGrid的名称是PG
。我需要在更改后刷新它。
此方法应该在用作PropertyGrid的SelectedObject
的类中。