C# Visual Studio - 继承控件的新“默认”属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7367/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 16:47:41  来源:igfitidea点击:

Visual Studio - new "default" property values for inherited controls

提问by Clyde

I'm looking for help setting a new default property value for an inherited control in Visual Studio:

我正在寻找帮助为 Visual Studio 中的继承控件设置新的默认属性值:

class NewCombo : System.Windows.Forms.ComboBox
{
  public NewCombo() { DropDownItems = 50; }
}

The problem is that the base class property DropDownItemshas a 'default' attribute set on it that is a different value (not 50). As a result, when I drag the control onto a form, the designer file gets an explicit mycontrol.DropDownItems = 50;line.

问题是基类属性DropDownItems上设置了一个不同的值(不是 50)的“默认”属性。结果,当我将控件拖到窗体上时,设计器文件会得到一条明确的mycontrol.DropDownItems = 50;行。

At first, this doesn't matter. But if later I change my inherited class to DropDownItems = 45;in the constructor, this does not affect any of the controls on any form since all those designer files still have the value 50 hard-coded in them. And the whole point was to have the value set in one place so I can deal with the customer changing his mind.

起初,这并不重要。但是,如果稍后我将继承的类更改为DropDownItems = 45;在构造函数中,这不会影响任何表单上的任何控件,因为所有这些设计器文件中的值 50 仍然是硬编码的。重点是将价值设置在一个地方,这样我就可以应对改变主意的客户。

Obviously, if I were creating my own custom property in the subclass, I could give it its own designer default attribute of whatever I wanted. But here I'm wanting to change the default values of properties in the base. Is there any way to apply Visual Studio attributes to a base class member? Or is there some other workaround to get the result I want?

显然,如果我在子类中创建我自己的自定义属性,我可以为它提供我想要的任何设计器默认属性。但是在这里我想更改基础中属性的默认值。有什么方法可以将 Visual Studio 属性应用于基类成员?或者是否有其他解决方法来获得我想要的结果?

采纳答案by Andrew Peters

In your derived class you need to either override (or shadow using new) the property in question and then re-apply the default value attribute.

在您的派生类中,您需要覆盖(或使用new隐藏)有问题的属性,然后重新应用默认值属性。