C# 如何过滤propertygrid
我需要在我的项目中使用propertygrid,并且我需要过滤将显示的属性。我找到了一种按类别筛选propertygrid的方法,但在筛选时需要更深入 以下是仅显示“外观”类别的代码。但我需要禁用“外观”下的一些属性,如“背景色”C# 如何过滤propertygrid,c#,winforms,propertygrid,C#,Winforms,Propertygrid,我需要在我的项目中使用propertygrid,并且我需要过滤将显示的属性。我找到了一种按类别筛选propertygrid的方法,但在筛选时需要更深入 以下是仅显示“外观”类别的代码。但我需要禁用“外观”下的一些属性,如“背景色” 我怎样才能过滤掉背景色呢?如果你想静态地禁用这个道具,例如在编译时,你可以试试。将它们设置为不可见运行时更为复杂,请查看。我通常使用此方法在PropertyGrid中动态显示/隐藏属性(使用反射): 参数包括: \u Who:要动态更改的属性名称 \u已启用:tru
我怎样才能过滤掉背景色呢?如果你想静态地禁用这个道具,例如在编译时,你可以试试。将它们设置为不可见运行时更为复杂,请查看。我通常使用此方法在PropertyGrid中动态显示/隐藏属性(使用
反射
):
参数包括:
- \u Who:要动态更改的属性名称
- \u已启用:true显示属性,false对PropertyGrid隐藏属性
PG
。更改后我需要刷新它
此方法应属于用作PropertyGrid的SelectedObject
的类
Attribute myfilterattribute = new CategoryAttribute("Appearance");
pg_1.BrowsableAttributes = new AttributeCollection(new Attribute[] { myfilterattribute });
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();
}