title: Two-way databinding in .NET does not work, even with INotifyPropertyChanged objects
body: <p>I'm working on a Windows Forms application, which of course involves databinding to objects.</p>
  
  <p>From what I've gathered, two-way databinding does not work out-of-the-box with .NET.</p>
  
  <p>My problem is that I can't find consistent information on implementing it :</p>
  
  <ul>
  <li><p>According to ".NET WinForms in a nutshell", I have to create an event for each property, called &lt;propertyName&gt;Changed, and fire it in the setter of the associated property. </p></li>
  <li><p>According to online sources (including MSDN), I simply need to implement INotifyPropertyChanged in my class, and fire the PropertyChanged event in all setters, passing in the name of the property.</p></li>
  </ul>
  
  <p>However, assuming I have a simple UI with a textbox databound to a property of a class (implementing either of the above methods), if I set the value of the databound property in code, the UI does not reflect the change, using either methods.</p>
  
  <p>So what am I missing here ?</p>
  
  <p><strong>Edit</strong> </p>
  
  <p>I should have mentionned that INotifyPropertyChanged is not implemented in the declaration of the concerned classes, but through dynamic code generation roughly equivalent to DynamicProxy. I'm creating a new question for that topic since the answer fits the actual question.</p>
  
  <p><strong>Another edit</strong> INotifyPropertyChanged is properly implemented by the code generation tool, I have checked that point by casting an instance of the proxy object to INotifyPropertyChanged, and registered an handler for the event, which does get raised when the value of a property is changed.</p>
  
  <p>UpdateMode of the databindings IS set to NotifyPropertyChanged, and CausesValidation is set to false on the associated control.</p>
  
  <p>So pseudo-code looks like this :</p>
  
  <pre><code>class MyForm {
      MyClass localObj;
      BindingSource mySource = new BindingSource();
  
      MyForm(MyClass obj) {
          localObj = obj.CreateProxy() // Ensures localObj is a proxy instance
          mySource.Datasource = localObj // After this, UI is updated accordingly
  
          localObj.someProperty = "I changed the value" // Here, the NotifyPropertyChanged event does get fired, but the UI does not update. Hence my question...
          Console.WriteLine(localObj.someProperty) // Output is 'I changed the value'
      }
  
  }
  </code></pre>
  
  <p>So the only way to get the UI to update is to call ResetBindings on the BindingSource... which kind of defeats the purpose of two way binding IMHO.</p>
  
  <p><strong>Finally !!</strong></p>
  
  <p>The problem comes from the Designer. At design-time, I don't have access to the real-type of the business object's proxy, which is a runtime type generated by the proxy (which is functionnal, I have no doubts about that part). So the BindingSource gets initialized by the designer using the known datatype of the business object, and not the datatype of the proxy (which is a class inheriting from the BO's class)</p>
  
  <p>Hence, the datasource needs to be initialized manually with the runtime-type of the proxy object to make two-way binding work properly.</p>
  
  <p>The working version is as follows (in pseudo C#) :</p>
  
  <pre><code>class MyForm {
      Object localObj;
      BindingSource mySource;
  
      MyForm(MyClass obj) {
          localObj = obj.CreateProxy() // Ensures localObj is a proxy instance
  
          ((ISupportInitialize) mySource).BeginInit()
          mySource.Datasource = localObj.GetType()
          ((ISupportInitialize) mySource).EndInit()
  
          mySource.Datasource = localObj // After this, UI is updated accordingly
  
          localObj.someProperty = "I changed the value" // UI updates ! :)
      }
  }
  </code></pre>
  
  <p><strong>Thank you to all for your advice and help, you just saved my day. I was banging my head against the wall on this one !</strong></p>