简体   繁体   English

如何在设计时 (C#) 隐藏一些默认控件属性?

[英]How do I hide some of the default control properties at design-time (C#)?

I have a custom control that I made.我有一个我制作的自定义控件。 It inherits from System.Windows.Forms.Control , and has several new properties that I have added.它继承自System.Windows.Forms.Control ,并添加了几个新属性。 Is it possible to show my properties (TextOn and TextOff for example) instead of the default "Text" property.是否可以显示我的属性(例如 TextOn 和 TextOff)而不是默认的“文本”属性。

My control works fine, I'd just like to de-clutter the property window.我的控件工作正常,我只想整理属性 window。

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.此外,您可以为您的类型使用自定义TypeConverter并覆盖GetProperties方法来控制为您的类型显示哪些属性。 This approach is more robust to the underlying base classes changing but can take more effort, depending on what you want to achieve.这种方法对底层基类的变化更加健壮,但可能需要更多的努力,具体取决于您想要实现的目标。

I often use a combination of the Browsable attribute and a custom TypeConverter .我经常结合使用Browsable属性和自定义TypeConverter

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.您可能还想添加[EditorBrowsable(EditorBrowsableState.Never)] ,这将在代码编辑器中隐藏 IntelliSense 中的属性。 Note that it will only be hidden in a separate solution from the original control.请注意,它只会隐藏在与原始控件不同的解决方案中。

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....以这种方式为我工作,它不会出现在设计器属性中,并且设计器不会初始化该属性,这将覆盖我自己的初始化......

You are looking for design-time attributes , specifically the BrowsableAttribute.您正在寻找设计时属性,特别是BrowsableAttribute。 DefaultPropertyAttribute sets which property is the default one to edit. DefaultPropertyAttribute设置哪个属性是要编辑的默认属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM