我有一个显示列表的属性网格,例如类 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);
}
}
问题是我如何在运行时控制 Browsable()
属性,这样当 ShowHidden = false
Hidden
行(突出显示为黄色下面)被省略。
谢谢。
最佳答案
这是一个例子:
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);
只需将 DataType
替换为您的属性名称。
请注意,所有属性都必须具有正在更改的属性(在本例中为 Browsable)。如果其中一个属性缺少该属性,所有 类属性将获得新的属性设置。
关于c# - 您如何控制运行时属性网格中的可见内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13673101/