Solved

Data binding control to object property

Posted on 2004-08-20
5
280 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
ID: 11854081
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
ID: 11854104
Oops!  I didn't realize that I was in VB... give the points to anyone that can convert that :)
0
 

Author Comment

by:Jetgunner
ID: 11854480
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
ID: 11855300
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
ID: 11855305
If you property is called "CustomProperty1", you must call the event "CustomProperty1Changed"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

22 Experts available now in Live!

Get 1:1 Help Now