【发布时间】:2017-03-16 05:48:59
【问题描述】:
有没有办法在“CollectionPropertiesEditor 的 PropertyGrid”中隐藏显示属性 最近我发现有一种方法可以在运行时更改 PropertyGrid 的 Browsable 属性。
我想知道这是否可以对“CollectionPropertiesEditor 的 PropertyGrid”进行,我在 Google 搜索上找不到相关结果。现在我希望 StackOverflow 能帮我解决这个问题。
问题:由于新的客户要求,我不得不向 GridColumn 控件添加一些属性。
[Category("Extra")]
[Browsable(true)]
public int? Position { get; set; }
[Category("Extra")]
[Browsable(true)]
public string storedColumn { get; set; }
我希望早点工作:
[Category("Extra")]
[Browsable(matchSomeRunTimeCondition)]
public int? Position { get; set; }
[Category("Extra")]
[Browsable(matchSomeRunTimeCondition)]
public string storedColumn { get; set; }
为什么它不起作用?
因为 Browsable Attribute 只能接受 Constant。而matchSomeRunTimeCondition 不是一个常数。用户可以在应用程序仍在运行时随时更改它。
在代码中,如果有一个函数可以让我在运行时使它们不可见,如果有人可以帮助我编写一个这样的函数或条件语句,我将非常感激:
如果(属性类别 == “额外”){
//不在propertygrid中显示该属性。
//或者换句话说,在运行时将 Browasable Attribute 设为 False。
}
在编译时,我将 Browsable 属性设置为 true,因为它需要在某些条件下可见。但是我需要一种机制来根据用户在运行时的选择来隐藏它。
通过在加载选定控件时进行设置,在 propertygrid 中解决了这个问题,如帖子所述:Make all properties with specific Category name invisible in PropertyGrid in c# Winforms at Runtime based on some condition
但是在我用来保存我的网格列的 CollectionPropertiesEditor 中没有这种奢侈(至少我不知道该怎么做)。
我将网格的所有网格列以 GridColumns 列表的形式存储为属性。
这就是我当前在 Grid 的属性中存储 GridColumns 的方式:
[Browsable(true)]
[Editor(typeof(CollectionPropertiesEditor), typeof(UITypeEditor))]
public List<TGridColumn> Columns { get; set; }
【问题讨论】:
-
您应该通过派生自
CustomTypeDescriptor或实现ICustomTypeDescriptor来编写自己的类型描述符。例如看看这篇文章:Combining multiple Attributes to a single Attribute -
嗨@RezaAghaei,很高兴见到你这么久。让我检查一下您的解决方案并返回(可能需要一些时间)。
-
嗨,也很高兴见到你:)
-
@RezaAghaei,我不明白如何使用您的解决方案来解决这个问题。
-
@RezaAghaei,您知道创建自定义 CollectionPropertiesEditor 是否需要花费太多精力吗?
标签: c# .net winforms windows-forms-designer propertygrid