Connecting Tech Pros Worldwide Forums | Help | Site Map

INotifyPropertyChanged and programmatic changes to GUI controls issue

Newbie
 
Join Date: Jan 2008
Posts: 5
#1: Jun 17 '09
Okay say we have a class that implements INotifyPropertyChanged and a property that has code in the setter to raise the PropertyChanged event. On the GUI side we have a text box with the code:

txtFirst.DataBindings.Add("Text", p, "FirstName", True) Where p is a Person object and FirstName is the property that raises the PropertyChanged event.

It works perfectly when I programmatically change the object's property or using the application, change the textbox's text. The problem is if I try to change the textbox's text programmatically.

Consider this example:

Saying txtFirst.Text = "Joe" (when it was previously blank) does not update the object's FirstName property. But if I write the following code it does:

txtFirst.Focus()
txtFirst.Text = "Joe"
txtLast.Focus()

Basically I emulated what a person using the program would do. Click on the textbox. Enter text, and then click on the next text box. It seems the loss of focus kicks off the PropertyChanged event. But this seems like a non-elegant solution. Any ideas on what I am doing wrong? Or is this what I have to do to get this to work

I'm using VB.Net 3.5 SP1 and a Smart Device project (thus, win forms). I'm trying to implement a n-tier program seperating the GUI code from the Logic and Logic form the data. Thus I don't want in the GUI to change the properties of the logic objects. I'd rather change the GUI control and have that due to 2-way binding update the logic object.

Thanks for your help!

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,777
#2: Jun 17 '09

re: INotifyPropertyChanged and programmatic changes to GUI controls issue


Quote:
It seems the loss of focus kicks off the PropertyChanged event. But this seems like a non-elegant solution.
Would you want to change the property (and thus raise the PropertyChanged event) on every keystroke by the user in the textbox? That seem ugly:

J <PropertyChangedEvent>
o <PropertyChangedEvent>
e <PropertyChangedEvent>
{space} <PropertyChangedEvent>
S <PropertyChangedEvent>
m <PropertyChangedEvent>
i <PropertyChangedEvent>
t <PropertyChangedEvent>
h <PropertyChangedEvent>

How about a local property in the form? Changing it changes the text of the textbox as well as your p object property?

Expand|Select|Wrap|Line Numbers
  1. string FirstName
  2. {
  3. get
  4. {
  5.    return p.FirstName;
  6. }
  7. set
  8. {
  9.    txtFirst.Text = value;
  10.    p.FirstName = value;
  11. }
  12. }
Newbie
 
Join Date: Jan 2008
Posts: 5
#3: Jun 17 '09

re: INotifyPropertyChanged and programmatic changes to GUI controls issue


I had a brain fart apparently. I have nothing against programmatically setting p.FirstName = Joe instead of the txtFirst.Text = Joe.

Thanks for the answer though as it makes sense why PropertyChanged does not fire when I set the text. Thanks.
Reply