简体   繁体   English

如何设置设计时属性默认值?

[英]How to set design-time property default values?

According to MSDN ( http://msdn.microsoft.com/en-us/library/system.windows.forms.label.autosize.aspx ), there's a note about Label 's AutoSize property: 根据MSDN( http://msdn.microsoft.com/en-us/library/system.windows.forms.label.autosize.aspx ),有一个关于LabelAutoSize属性的说明:

When added to a form using the designer, the default value is true. 使用设计器添加到表单时,默认值为true。 When instantiated from code, the default value is false. 从代码实例化时,默认值为false。

The question is: how can I override a Label control and set its AutoSize property's design-time default value to false ? 问题是:如何覆盖Label控件并将其AutoSize属性的设计时默认值设置为false

(Update) (更新)

And this doesn't work: 这不起作用:

class MyLabel : Label
{
    const bool defaultAutoSize = false;

    public MyLabel()
    {
        AutoSize = defaultAutoSize;
    }

    [DefaultValue(defaultAutoSize)]
    public override bool AutoSize
    {
        get
        {
            return base.AutoSize;
        }
        set
        {
            base.AutoSize = value;
        }
    }
}

Simply use inheritance. 只需使用继承。 You will have to use your custom Label instead of the System one, though. 但是,您必须使用自定义标签而不是系统标签。

public class MyLabel:Label
{
    public MyLabel():base()
    {
        base.AutoSize = false;
    }
}

You can put this directly into your code and modify the code like below. 您可以将其直接放入代码中并修改代码,如下所示。 Or you can put this class into its own library, which you should then be able to load into the toolbox and use like any other component. 或者,您可以将此类放入其自己的库中,然后您可以将其加载到工具箱中并像使用任何其他组件一样使用。

For this to work from the toolbox, it seems that you need to override the InitLayout, like below, and add an attribute to the AutoSize property so it is not serialized into the designer: 为了在工具箱中工作,您似乎需要覆盖InitLayout,如下所示,并向AutoSize属性添加一个属性,以便它不会序列化到设计器中:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [DefaultValue(false)]
    public override bool AutoSize
    {
        get
        {
            return base.AutoSize;
        }
        set
        {
            base.AutoSize = value;
        }
    }

    protected override void InitLayout()
    {
        base.InitLayout();
        base.AutoSize = false;
    }

If you are not using the toolbox, then once you drop your normal label onto your form, you need to go into the [Form].Designer.cs and find and modify your labels: 如果您没有使用工具箱,那么一旦将正常标签放到表单上,就需要进入[Form].Designer.cs并找到并修改标签:

this.label1 = new MyLabel();// new System.Windows.Forms.Label();

//this.label1.AutoSize = true;

You have to remove the preset AutoSize property because when you drop the label it sets it in the designer, and even if you change the label instantiation to be your type, the manual AutoSize set will override your default 您必须删除预设的AutoSize属性,因为当您删除标签时,它会将其设置在设计器中,即使您将标签实例化更改为您的类型,手动AutoSize设置也将覆盖您的默认值

The Label control has an attribute: Label控件有一个属性:

[ToolboxItem("System.Windows.Forms.Design.AutoSizeToolboxItem,System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]

which cause the strange AutoSize problem. 这导致奇怪的AutoSize问题。

I can disable it by this: 我可以通过这个禁用它:

[ToolboxItem(true)]
class MyLabel : Label
{
}

See the DefaultValueAttribute 请参见DefaultValueAttribute

Like so: 像这样:

public class MyLabel : Label
{
    [System.ComponentModel.DefaultValue(false)]
    public override bool AutoSize
    {
        get
        {
            return base.AutoSize;
        }
        set
        {
            base.AutoSize = value;
        }
    }
}

EDIT: This does not work as expected. 编辑:这不能按预期工作。 Tung's answer is correct. 董的答案是对的。 ...Wrong again. ...又错了。

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

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