根据某些条件,在运行时在 c# Winforms 中的 PropertyGrid 中使具有特定类别名称的所有属性不可见 [英] Make all properties with specific Category name invisible in PropertyGrid in c# Winforms at Runtime based on some condition
问题描述
我的应用程序中有一个属性 Grid,它在运行时显示选定控件的属性.
I have a property Grid in my application which shows the properties of a selected control at run time.
最近需要为已经存在的控件添加一些额外的属性.为了适应这种情况,引入了一个新类别,在该类别下添加了较新的属性.
Recently there was a requirement to add some extra properties to the already existing controls. To accommodate this a new category was introduced under which the newer properties were added.
现在我正在寻找一种机制,可以根据应用程序中的运行时条件在运行时隐藏这些新添加的属性.但我对此没有合适的解决方案.
Now I am looking for a mechanism to hide these newly added properties at run time based on a runtime condition in the application. But I don’t have a proper solution for this.
新增代码:
[ReadOnly(true)]
[Browsable(true)]
[Category("Extra")]
public int? FormID { get; set; }
[Browsable(true)]
[Category("Extra")]
public int? jrxml_Id { get; set; }
我期待的工作:
[ReadOnly(true)]
[Browsable(matchSomeRunTimeCondition)]
[Category("Extra")]
public int? FormID { get; set; }
[Browsable(matchSomeRunTimeCondition)]
[Category("Extra")]
public int? jrxml_Id { get; set; }
为什么它不起作用?
因为Browsable
属性只能接受Constant.而 matchSomeRunTimeCondition
不是一个常数.用户可以在应用程序仍在运行时随时更改它.
Because Browsable
Attribute can only accept Constant. And matchSomeRunTimeCondition
is not a constant. A user can change it when-ever he wants while the application is still running.
在代码中,如果有一个函数可以让我在运行时使这些不可见,如果有人能帮我写一个这样的函数或条件语句,我会非常高兴:
In code if there is a function that I can use to make these invisible at run-time I will be really greatful if someone can help me write one such function or conditional statement like so:
如果(属性的类别==额外"){
If (property’s category == "Extra") {
//在propertygrid中不显示该属性.
//Do not show this property in the propertygrid.
//或者换句话说,在运行时将Browasable Attribute设为False.
//Or in other words, make Browasable Attribute False at run time.
}
我的应用程序中的不同控件具有需要在运行时隐藏的不同属性.
Different controls in my application have different properties that need to be hidden at run time.
我有一个所有属性的列表,我想在应用程序中选择的每个对象中隐藏这些属性以显示在属性网格中.
I have a list of all those properties which I want to hide in each of the object selected in the application to be shown in the propertygrid.
但我需要关于如何实现这一目标的帮助.
But I need help in how to achieve this.
注意:我愿意在编译时将可浏览属性设置为 true/false.但是我想要一种机制来在我的应用程序中在运行时设置它,并且需要帮助来做到这一点.
Note: I am open to setting the browsable property to true / false at compile time. But I want a mechanism to set this at run time in my application and need help in doing this please.
寻找代码解决方案.
我的编码环境(由于公司遗留支持无法更改):
My coding environment (cannot change due to companies legacy support):
- Visual Studio 2010
- .Net 3.5
- Windows 10 操作系统(是的,我知道)
推荐答案
使用 TnTinMn 发布的答案,我将代码修改如下,使其在运行时隐藏所有选择性属性.
using the answer posted by TnTinMn, I modified my code as follows to make it hide all selective properties at run time.
private void ChangeVisibilityBasedOnMode( ref object[] v) {
if (matchSomeRunTimeCondition) {
List<string> PropertiesToHide = new List<string> {
"FormID",
"jrxml_Id",
};
foreach (var vObject in v) {
var properties = GetProperties(vObject);
foreach (var p in properties) {
foreach (string hideProperty in PropertiesToHide ) {
if (p.Name.ToLower() == hideProperty.ToLower()) {
setBrowsableProperty(hideProperty, false, vObject);
}
}
}
}
}
}
private void setBrowsableProperty(string strPropertyName, bool bIsBrowsable, object vObject) {
try {
PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties(vObject.GetType())[strPropertyName];
BrowsableAttribute theDescriptorBrowsableAttribute = (BrowsableAttribute)theDescriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrowsable = theDescriptorBrowsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance);
isBrowsable.SetValue(theDescriptorBrowsableAttribute, bIsBrowsable);
} catch (Exception) { }
}
还要感谢 neoikon 的精彩帖子(条件可浏览"属性),我从中得出了最终解决方案.
Also thanks to neoikon for his wonderful post (Conditional "Browsable" Attribute) from which I reached the final solution.
这篇关于根据某些条件,在运行时在 c# Winforms 中的 PropertyGrid 中使具有特定类别名称的所有属性不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!