In Designer: Property default value is set, but property isn't called while setting default?

前端 未结 2 522
予麋鹿
予麋鹿 2021-01-20 06:41

I have a piece of code that goes something like this:

[DefaultValue(false)]
public bool Property
{
    set
    {
        blah = value;
        someControl.Vi         


        
2条回答
  •  北海茫月
    2021-01-20 07:36

    The [DefaultValue] attribute is only a hint to the designer and the serializer. It is completely up to you to ensure that the default value you promised in the attribute is in fact the value of the property. The property setter doesn't get called in your case because the serializer detected that the current value of the property equals the default value and can thus omit the property assignment. It is an optimization, it makes InitializeComponent() smaller and faster.

    You ensure this simply by initializing the property value in your constructor. Beware that a control's Visible property defaults to true.

提交回复
热议问题