如何在设计时 (C#) 隐藏一些默认控件属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/615791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I hide some of the default control properties at design-time (C#)?
提问by Krakerjak
I have a custom control that I made. It inherits from System.Windows.Forms.Control
, and has several new properties that I have added. Is it possible to show my properties (TextOn and TextOff for example) instead of the default "Text" property.
我有一个自定义控件。它继承自System.Windows.Forms.Control
,并具有我添加的几个新属性。是否可以显示我的属性(例如 TextOn 和 TextOff)而不是默认的“Text”属性。
My control works fine, I'd just like to de-clutter the property window.
我的控件工作正常,我只想清理属性窗口。
采纳答案by Jeff Yates
You could either override them (if they can be overriden) and apply the Browsable
attribute, specifying false
, or create a new version of the property and apply the same attribute (this second approach doesn't always appear to work so YMMV).
您可以覆盖它们(如果它们可以被覆盖)并应用Browsable
属性,指定false
,或者创建属性的新版本并应用相同的属性(第二种方法似乎并不总是有效,所以 YMMV)。
Also, you can use a custom TypeConverter
for your type and override the GetProperties
method to control what properties get displayed for your type. This approach is more robust to the underlying base classes changing but can take more effort, depending on what you want to achieve.
此外,您可以TypeConverter
为您的类型使用自定义并覆盖该GetProperties
方法来控制为您的类型显示哪些属性。这种方法对于底层基类的变化更加健壮,但可能需要更多的努力,这取决于您想要实现的目标。
I often use a combination of the Browsable
attribute and a custom TypeConverter
.
我经常使用Browsable
属性和自定义的组合TypeConverter
。
回答by Eric Lathrop
You are looking for design-time attributes, specifically the BrowsableAttribute.DefaultPropertyAttributesets which property is the default one to edit.
您正在寻找设计时属性,特别是BrowsableAttribute。DefaultPropertyAttribute设置哪个属性是要编辑的默认属性。
回答by SLaks
Override the property and add [Browsable(false)]
.
覆盖该属性并添加[Browsable(false)]
.
You might also want to add [EditorBrowsable(EditorBrowsableState.Never)]
, which will hide the property in IntelliSense in the code editor. Note that it will only be hidden in a separate solution from the original control.
您可能还想添加[EditorBrowsable(EditorBrowsableState.Never)]
,这将隐藏代码编辑器中 IntelliSense 中的属性。请注意,它只会隐藏在与原始控件不同的解决方案中。
回答by womd
using System.ComponentModel;
使用 System.ComponentModel;
[Browsable(false), DesignerSerializationVisibility(
DesignerSerializationVisibility.Hidden)]
public int MyHiddenProp {get; set; }
worked for me in that way, that it would not appear in designer-properties, AND the propertiy will not be initialized by the designer, which would override my own initialisation....
以这种方式为我工作,它不会出现在设计器属性中,并且设计器不会初始化该属性,这将覆盖我自己的初始化......