1

I have the following Properties

[DefaultValue(true), Category("Behavior")]
public bool EnableBinding { get; set; }        

[DefaultValue(false), Category("Behavior")]
public bool NeedApprove { get; set; }

When changed using the designer and save then rebuild, the new values you set through designer will remain only with the property NeedApprove. EnableBinding is always getting reset to false.

Tried

1) DesignerSerializationVisibility attributes, but didnt work!

  • Visible
  • Hidden
  • Content

2) Converting auto property to full property This worked. But why? Can not we achieve this without converting to a full property?

1 Answers1

1

You should assign initial value for the EnableBinding property within your custom user-control constructor:

public partial class CustomUserControl : UserControl {
    public CustomUserControl() {
        InitializeComponent();
        EnableBinding = true; // !!!
    }
    [DefaultValue(true), Category("Behavior")]
    public bool EnableBinding { get; set; }
    [DefaultValue(false), Category("Behavior")]
    public bool NeedApprove { get; set; }
}

Without that it will be always initialized as false during deserialization.

DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • Hi DmitryG, Thanks for the answer. But why do we need to specifically mention the default value either in the constructor (as you have shown) or converting to an full property? There is a default value given within the attribute [DefaultValue(false)]. Isn't it? – Kasun Wanniarachchi Jan 27 '16 at 11:02
  • The DefaultValue attribute does not affect the real value of target property. It only rule for Design-Time infrastructure... – DmitryG Jan 27 '16 at 15:13
  • Hi DmitryG Thanks a lot :) This link https://msdn.microsoft.com/en-us/library/ms171834.aspx also describes why Designer Serialization is different than normal Binary / XML / SOAP / JSON Serializations in .Net – Kasun Wanniarachchi Jan 28 '16 at 12:50