Solved

Data binding control to object property

Posted on 2004-08-20
5
282 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

17 Experts available now in Live!

Get 1:1 Help Now