In Designer: the value of the property is set by default, but the property is not called when the default setting?

I have some code that looks something like this:

[DefaultValue(false)]
public bool Property
{
    set
    {
        blah = value;
        someControl.Visible = value;
    }
    get
    {
        return blah;
    }
}

When I look at properties in the constructor, the property is false (or true if I play with it).
But the property is not really set. The Visible Control value does not change.

How can I force the constructor to actually set a property with a default value?

+3
source share
2 answers

[DefaultValue] - . , , , , . , , . , InitializeComponent() .

. , Visible true.

+3

:

  • ( bool false)
  • use ShouldSerialize *, .

, bool? " " " ":

private bool? property;
public bool Property
{
    set
    {
        property = value;
        someControl.Visible = value;
    }
    get
    {
        return property.GetValueOrDefault();
    }
}
public void ResetProperty() { property = null; }
public bool ShouldSerializeProperty() { return property.HasValue; }

, Reset * ShouldSerialize * , .

+1

All Articles