如何在运行时控制属性网格中可见的内容?

ja7*_*a72 8 c# propertygrid winforms

我有一个属性网格显示一个列表,例如一个类 Person

[TypeConverter(typeof(ExpandableObjectConverter))]
public class Person
{
    public bool ShowHidden { get; set; }
    public string Name { get; set; }
    //[Browsable(false)]
    public string Hidden { get; set; }

    public override string ToString()
    {
        return string.Format("Person({0})", Name);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是如何控制的Browsable()在运行时,使得属性时ShowHidden = falseHidden省略线(突出显示下面黄色).

截图

谢谢.

Neo*_*isk 12

这是一个例子:

PropertyDescriptor descriptor=
  TypeDescriptor.GetProperties(this.GetType())["DataType"];
BrowsableAttribute attrib= 
  (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)]; 
FieldInfo isBrow = 
  attrib.GetType().GetField("browsable",BindingFlags.NonPublic | BindingFlags.Instance);
isBrow.SetValue(attrib,false);
Run Code Online (Sandbox Code Playgroud)

只需替换DataType您的属性名称即可.请注意,所有属性都必须更改属性(在本例中为Browsable).如果其中一个属性缺少该属性,则所有类属性都将获取新属性设置.

从这里获取的代码:探索属性网格的行为.

  • 代码更改了BrowsableAttribute,但我没有看到网格中的更改...我怎样才能看到更改运行时? (2认同)