Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7718357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:08:56+00:00 2026-06-01T03:08:56+00:00

I’m working on a Windows Forms application, which of course involves databinding to objects.

  • 0

I’m working on a Windows Forms application, which of course involves databinding to objects.

From what I’ve gathered, two-way databinding does not work out-of-the-box with .NET.

My problem is that I can’t find consistent information on implementing it :

  • According to “.NET WinForms in a nutshell”, I have to create an event for each property, called <propertyName>Changed, and fire it in the setter of the associated property.

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

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.

So what am I missing here ?

Edit

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.

Another edit 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.

UpdateMode of the databindings IS set to NotifyPropertyChanged, and CausesValidation is set to false on the associated control.

So pseudo-code looks like this :

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'
    }

}

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.

Finally !!

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)

Hence, the datasource needs to be initialized manually with the runtime-type of the proxy object to make two-way binding work properly.

The working version is as follows (in pseudo C#) :

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 ! :)
    }
}

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 !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:08:57+00:00Added an answer on June 1, 2026 at 3:08 am

    I would check if the dynamic code generation actually adds INotifyChanged before the form is loaded. Then I would check if the public property of the object that you change by code is actually the same object as the one that is currently displayed on the screen (is it a list or a single object?). Make sure the databinding is set to OnPropertyChanged instead of OnValidation. Then I would check if it works the other way around: edit the databound textbox-value and see if the object gets the new value after you select another control. Be carefull when you change the property with code like this (pseudocode).

    ctor(MyClass objParm){
      this.localobj = objParm;
      bindingsource1.datasource = objParm; // or this.localobj
    }
    SomeMethod(){
      this.localobj = new MyClass();
      this.localobj.MyProperty = 'new value';
    }
    

    This will not work, except when you change to

    SomeMethod(){
      this.localobj = new MyClass();
      this.localobj.MyProperty = 'new value';
      bindingsource1.datasource = this.localobj
    }
    

    Update:
    You say that databinding does not work out of the box, but I can ensure you it does. After seeing the code your provided I suspect it has to do with the dynamic proxy generation / CreateProxy()-method. Since ResetBindings works, it probably has to do with the fact that the CreateProxy returns a different class then MyClass. Databinding is very picky concerning the types. E.g. if you use an interface, such as List for the datasource, Databinding can give errors because they are not the same types. That’s why you need to use new BindingList() as a datasource. Just giving you some background information that might point you in the right direction. In your case, the metadata probably changes due to the dynamic code generation, have a look in the msdn documentation on ResetBindings(). As a test try this:

    mySource.Datasource = new BindingList<MyClass>(new List<MyClass>(){this.localobj});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.