Search code examples
c#.netcustom-controlscustom-properties

Custom Property of a Custom User Control gets reset during rebuild


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?


Solution

  • 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.