title: Class type property breaks wired event
body: <p>I have struggled with this for almost a day now, and have boiled it down to the following:</p>
  
  <p>I have a UserControl called MyUserControl: </p>
  
  <pre><code>public partial class MyUserControl : UserControl
  {
  …
  </code></pre>
  
  <p>This UserControl has one property called Property1: </p>
  
  <pre><code>public MySubClass Property1 {get; set;}
  </code></pre>
  
  <p>As you see the property is of type MySubClass which is a class defined as seen here:</p>
  
  <pre><code>[TypeConverter(typeof(ExpandableObjectConverter))]
  public class MySubClass : INotifyPropertyChanged
  {
      //Declare event fired when a property has changed
      public event PropertyChangedEventHandler PropertyChanged;
  
      private double _subClassProperty1;
  
      public double SubClassProperty1
      {
          get
          {
              return _subClassProperty1;
          }
          set
          {
              _subClassProperty1 = value;
              OnPropertyChanged(); //Call method to raise event
          }
      }
  
      //Raises the PropertyChanged event
      protected void OnPropertyChanged()
      {
          PropertyChangedEventHandler handler = PropertyChanged; //Copy event handler to temporary variable to make it thread safe
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(""));
          }
      }
  }
  </code></pre>
  
  <p>Note that this class contains an event that is fired when a property has been changed inside the class (in this case only one property exists: SubClassProperty1).</p>
  
  <p>In the constructor of the UserControl I subscribe to the event from the sub class to get notified when a property has been changed. The constructor looks like this:</p>
  
  <pre><code>public MyUserControl()
  {
      InitializeComponent();
  
      Property1 = new MySubClass();
  
      //Wire event fired when a property has been changed in sub class
      Property1.PropertyChanged += MySubClass_PropertyChanged;
  }
  </code></pre>
  
  <p>The method (located in the user control class) called when the event is fired looks like this:</p>
  
  <pre><code>//Fired when a property has been changed in the sub class
  private void MySubClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
      MessageBox.Show("Property changed");
  }
  </code></pre>
  
  <p><strong>The problem is the following:</strong>
  When dragging the user control to a form and changing the property of the subclass (SubClassProperty1) the event is fired and the MessageBox shown (the change of the property value is done at design-time in the properties window of Visual Studio).
  If I now rebuild the project (Build-> Rebuild Solution) the event binding that I do in the constructor is lost, so when changing the value of SubClassProperty1 (again at design-time) the event is no more fired.</p>
  
  <p>If I make the setter of Property1 private like this: </p>
  
  <pre><code>public MySubClass Property1 {get; private set;}
  </code></pre>
  
  <p>the event binding is not lost when rebuilding the project, but now the value of SubClassProperty1 is not persisted (gets reset to the default value when rebuilding the project).</p>
  
  <p><strong>Question:</strong>
  I am clearly missing something about how to correct implement a class type property, but what am I doing wrong?</p>
  
  <p>To sum up: I want to be able to set the properties of the sub class through the user control class’ property from the properties window of Visual Studio, without loosing the event binding.</p>