你如何控制哪些在运行时属性网格是可见的? [英] How do you control what is visible in a property grid at runtime?
问题描述
我有一个属性网格显示列表,例如一类人
I have a property grid displaying a list, for example of a class 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);
}
}
现在的问题是我怎么控制可浏览()
在运行时的属性,这样当 ShowHidden = FALSE
的隐藏
线(以下高亮黄色)省略。
The question is how do I control the Browsable()
attribute at runtime such that when ShowHidden = false
the Hidden
line (highlighted yellow below) is omitted.
感谢
推荐答案
下面是一个例子:
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);
只需更换数据类型
与您的属性名称。
注意,所有属性必须被更改属性(在这种情况下,可在线浏览)。从这里采取了一个属性缺少类属性的属性,所有获得新的属性设置
Just replace DataType
with your property name.
Note, all properties must have the attribute being changed (in this case, Browsable). If one of the properties is missing the attribute, all of the class properties get the new attribute setting.
代码:<一HREF =http://www.codeproject.com/Tips/48015/Exploring-the-Behaviour-of-Property-Grid相对=nofollow>探索属性网格的行为。
这篇关于你如何控制哪些在运行时属性网格是可见的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!