Solved

Data binding control to object property

Posted on 2004-08-20
5
274 Views
Last Modified: 2011-09-20
I have a custom object with several properties.  When I bind the controls (e.g. textbox's text property) to a property of the object, it works fine.  However, when I update the object in code (change the properties programmatically), the changes are not reflected in the bound controls.  Is there a way to cause the changes to the property to be propagated to the controls?
0
Comment
Question by:Jetgunner
  • 4
5 Comments
 
LVL 10

Expert Comment

by:eternal_21
Comment Utility
In you custom object you need this code:

  public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

and when you change a property (e.g. MyProperty), you raise a PropertyChanged event:

    private object myProperty;
    public object MyProperty
    {
      get { return myProperty; }
      set
      {
        if(myProperty!=value)
        {
          myProperty = value;

          if(PropertyChanged!=null)
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("MyProperty"));
        }
      }
    }
0
 
LVL 10

Expert Comment

by:eternal_21
Comment Utility
Oops!  I didn't realize that I was in VB... give the points to anyone that can convert that :)
0
 

Author Comment

by:Jetgunner
Comment Utility
I added that code to my application (in vb of course).  When I trace through the app, I can see that the PropertyChanged  event does get raised when I change a property, but that doesn't seem to change the bound fields.
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 125 total points
Comment Utility
Can you give this a try?  Change your code:

  public event System.EventHandler MyPropertyChanged;

And then call:

  if(MyPropertyChanged!=null)
     MyPropertyChanged(this, new System.EventArgs());
0
 
LVL 10

Expert Comment

by:eternal_21
Comment Utility
If you property is called "CustomProperty1", you must call the event "CustomProperty1Changed"
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now