Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何过滤propertygrid_C#_Winforms_Propertygrid - Fatal编程技术网

C# 如何过滤propertygrid

C# 如何过滤propertygrid,c#,winforms,propertygrid,C#,Winforms,Propertygrid,我需要在我的项目中使用propertygrid,并且我需要过滤将显示的属性。我找到了一种按类别筛选propertygrid的方法,但在筛选时需要更深入 以下是仅显示“外观”类别的代码。但我需要禁用“外观”下的一些属性,如“背景色” 我怎样才能过滤掉背景色呢?如果你想静态地禁用这个道具,例如在编译时,你可以试试。将它们设置为不可见运行时更为复杂,请查看。我通常使用此方法在PropertyGrid中动态显示/隐藏属性(使用反射): 参数包括: \u Who:要动态更改的属性名称 \u已启用:tru

我需要在我的项目中使用propertygrid,并且我需要过滤将显示的属性。我找到了一种按类别筛选propertygrid的方法,但在筛选时需要更深入

以下是仅显示“外观”类别的代码。但我需要禁用“外观”下的一些属性,如“背景色”


我怎样才能过滤掉背景色呢?

如果你想静态地禁用这个道具,例如在编译时,你可以试试。将它们设置为不可见运行时更为复杂,请查看。

我通常使用此方法在PropertyGrid中动态显示/隐藏属性(使用
反射
):

参数包括:

  • \u Who:要动态更改的属性名称
  • \u已启用:true显示属性,false对PropertyGrid隐藏属性
我使用的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();
}