有没有一种方法可以使“可浏览”属性具有条件,这样应用它的属性有时会出现在属性页中,有时不会?
谢谢:)
发布于 2011-01-14 19:31:24
没有简单的方法。
你可以通过实现ICustomTypeDescriptor来解决这个问题。这里有一篇关于implementing ICustomTypeDescriptor的好文章。
或者,您可以将自己的ControlDesigner与您的类相关联,并覆盖PreFilterProperties方法以添加或删除在属性网格中查看的属性。
发布于 2013-11-19 11:04:00
我不确定这是否适用于您的情况,但您可以在运行时通过调用下面的函数来调整“可浏览”装饰。
/// <summary>
/// Set the Browsable property.
/// NOTE: Be sure to decorate the property with [Browsable(true)]
/// </summary>
/// <param name="PropertyName">Name of the variable</param>
/// <param name="bIsBrowsable">Browsable Value</param>
private void setBrowsableProperty(string strPropertyName, bool bIsBrowsable)
{
// Get the Descriptor's Properties
PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties(this.GetType())[strPropertyName];
// Get the Descriptor's "Browsable" Attribute
BrowsableAttribute theDescriptorBrowsableAttribute = (BrowsableAttribute)theDescriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrowsable = theDescriptorBrowsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance);
// Set the Descriptor's "Browsable" Attribute
isBrowsable.SetValue(theDescriptorBrowsableAttribute, bIsBrowsable);
}发布于 2011-01-14 19:31:08
您可以通过提供自定义类型模型来做到这一点;在最简单的级别上,您可以为从ExpandableObjectConverter派生的类型提供自定义TypeDescriptor,并且只需随意包含/排除给定的属性-但这只适用于属性页使用的PropertyGrid。一种更复杂的方法是使用ICustomTypeDescriptor / TypeDescriptionProvider -这样就可以在DataGridView等内部工作
https://stackoverflow.com/questions/4690481
复制相似问题