首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >条件"Browsable“属性

条件"Browsable“属性
EN

Stack Overflow用户
提问于 2011-01-14 19:20:25
回答 6查看 12.3K关注 0票数 19

有没有一种方法可以使“可浏览”属性具有条件,这样应用它的属性有时会出现在属性页中,有时不会?

谢谢:)

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-01-14 19:31:24

没有简单的方法。

你可以通过实现ICustomTypeDescriptor来解决这个问题。这里有一篇关于implementing ICustomTypeDescriptor的好文章。

或者,您可以将自己的ControlDesigner与您的类相关联,并覆盖PreFilterProperties方法以添加或删除在属性网格中查看的属性。

Removing certain properties from property grid.

票数 8
EN

Stack Overflow用户

发布于 2013-11-19 11:04:00

我不确定这是否适用于您的情况,但您可以在运行时通过调用下面的函数来调整“可浏览”装饰。

代码语言:javascript
复制
/// <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);
}
票数 11
EN

Stack Overflow用户

发布于 2011-01-14 19:31:08

您可以通过提供自定义类型模型来做到这一点;在最简单的级别上,您可以为从ExpandableObjectConverter派生的类型提供自定义TypeDescriptor,并且只需随意包含/排除给定的属性-但这只适用于属性页使用的PropertyGrid。一种更复杂的方法是使用ICustomTypeDescriptor / TypeDescriptionProvider -这样就可以在DataGridView等内部工作

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4690481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档