以编程方式隐藏 PropertyGrid 中的属性

发布于 2024-07-15 13:15:40 字数 504 浏览 7 评论 0原文

用于

<System.ComponentModel.TypeConverter(GetType(System.ComponentModel.ExpandableObjectConverter))> _

由多个属性组成的类(这是另一个类的属性)的声明。

我只需加载此类的实例...

PropertyGrid1.SelectedObject = oColumn

显然我不想在代码中手动构建 propertygrid,我知道如何做到这一点。

但问题就在这里。 根据属性的值,某些其他属性不应该是可见的,就像我

<System.ComponentModel.Browsable(False)> _

在属性声明中使用了该属性一样。

无论如何,是否可以以编程方式执行此操作,而不必手动处理属性网格的所有构建>

Using

<System.ComponentModel.TypeConverter(GetType(System.ComponentModel.ExpandableObjectConverter))> _

on the declaration of a class (which is a property of another class) that consists of a number properties.

I load an instance of this class with simply ...

PropertyGrid1.SelectedObject = oColumn

Obviously I don't want to manually build the propertygrid in code, I know how to do that.

But here's the problem. Depending on the value of a property, certain other properties should not be visible, as though I'd used the

<System.ComponentModel.Browsable(False)> _

attribute on the property declaration.

Is there anyway to do this programmatically, without having to handle all the building of the property grid manually>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梨涡少年 2024-07-22 13:15:40

其实这是完全有可能的。 第一种也是最简单的方法是设置网格的 BrowsableAttributes 属性:

propGraph.BrowsableAttributes = new AttributeCollection(
    new Attribute[] 
    { 
        new CategoryAttribute("Appearance")
    });

这将过滤掉与您提供的属性类型不匹配的所有属性。 不幸的是,这是一个正过滤器而不是负过滤器,这使得它不太有用。

其次,同样简单的是,您可以围绕要在 PropertyGrid 中显示的对象创建一个简单的包装器,并重新定义要隐藏的任何属性等。 作为直通属性:

public class MyDerivedControl : public TextBox
{
    [Browsable(false)]
    [Category("MyCustomCategory")]
    public new bool Enabled
    {
         get { return base.Enabled }
         set { base.Enabled = value; }
    }
}

将其弹出到属性网格中,并且 Enabled 属性将被隐藏。

第三,您可以自定义 PropertyGrid 本身并进入类型描述符的世界等等,但如果您只想隐藏几个属性,那就太过分了。

希望这可以帮助。

Actually this is entirely possible. The first and easiest way is to set the grid's BrowsableAttributes property:

propGraph.BrowsableAttributes = new AttributeCollection(
    new Attribute[] 
    { 
        new CategoryAttribute("Appearance")
    });

This will filter out all properties that do NOT match the attribute-types you supply. Unfortunately this is a positive filter rather than a negative filter which makes it less useful IMHO.

Second, and equally easy, you can create a simple wrapper around the object you want to display in the PropertyGrid and re-define whatever properties you want to hide/etc. as passthrough properties:

public class MyDerivedControl : public TextBox
{
    [Browsable(false)]
    [Category("MyCustomCategory")]
    public new bool Enabled
    {
         get { return base.Enabled }
         set { base.Enabled = value; }
    }
}

Pop that into a property grid and the Enabled property will be hidden.

Third, you can customize the PropertyGrid itself and get into the world of type descriptors and so forth, but if all you want to do is hide a couple properties, this is overkill.

Hope this helps.

羞稚 2024-07-22 13:15:40

如果您希望使用 gridItem.Hide() ,那么答案是否定的。 在 MS PropertyGrid 中实现这一点的唯一方法是通过 TypeConverter 或自定义类型描述符(实现 ICustomTypeDescriptor)的 GetProperties 方法动态发布属性。 我会首先尝试使用 TypeConverter(特别是如果您要检查的属性值处于同一级别),需要编写的代码较少。

if you were hoping for a gridItem.Hide() then, the answer is no. The only way to achieve that in the MS PropertyGrid is to dynamically publish your properties through the GetProperties method of a TypeConverter or custom type descriptor (that implements ICustomTypeDescriptor). I would try first with the TypeConverter (especially if the property values you want to check are at the same level), there is less coding to do.

水晶透心 2024-07-22 13:15:40

对于 C++,这里有一个简单的解决方案,可以在 propertyGrid 中显示选定的类别。

cli::array<Attribute^,1>^ attrs = {gcnew CategoryAttribute("Appearance")};
this->PropertyGrid->BrowsableAttributes = gcnew AttributeCollection(attrs);
this->PropertyGrid->SelectedObject = this->SelectedControl;

这将仅在 propertyGrid 中显示“外观”类别并隐藏所有其他类别。
假设您可以自己翻译 C# 代码。

As for the C++, here is an easy solution to show selected category in a propertyGrid.

cli::array<Attribute^,1>^ attrs = {gcnew CategoryAttribute("Appearance")};
this->PropertyGrid->BrowsableAttributes = gcnew AttributeCollection(attrs);
this->PropertyGrid->SelectedObject = this->SelectedControl;

This will show only 'Appearance' category in the propertyGrid and hide all other categories.
Assuming you can translate the code in C# yourself.

江心雾 2024-07-22 13:15:40

此问题类似,但答案更完整。 有些人可能希望交叉参考。

This Question is similar, but the answers are more complete. Some people may wish to cross reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文