Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF RaisePropertyChanged event on lost focus

Tags:

c#

mvvm

wpf

I have a C# WPF MVVM application that works fine.

The only problem is when I modify a textbox and click on the menu. If I do that without clicking on another control, the view->viewmodel event is never fired because the textbox hasn't lost focus. Correct me if I am wrong, but I think the RaisePropertyChanged is only fired on LostFocus (or OnBlur, or any similar event).

So, clicking on the menu save button right after editing the textbox causes the viewmodel to save the data using old values.

So, resuming:

This sequence works fine:

  1. Edit the text box
  2. Click on another control
  3. RaisePropertyChanged is fired, the viewmodel is updated
  4. Click on save button on the menu
  5. Data Saved with correct values

This sequence gives me an error:

  1. Edit the text box
  2. Click on save button on the menu
  3. Data Saved with correct values

How to solve this?

like image 359
guilhermecgs Avatar asked Jul 03 '13 20:07

guilhermecgs


People also ask

How do I track changes in a WPF property?

If your property is a custom dependency property, or if you're working with a derived class where you've defined the instantiation code, the WPF property system has a better way to track property changes. That way is to use the built-in CoerceValueCallback and PropertyChangedCallback property system callbacks.

How does the lostfocus event work?

You enter the first tb 2. You don't enter anything and you click the second one 3. The lostfocus event of tb1 fires and it opens a dialog, and focuses on tb1 again 4. because tb1 got focus, tb2's lostfocus event fires and it does the same, returning focus to tb2 and going on in an infinite loop.

What is a WPF event?

Windows Presentation Foundation (WPF) defines several events that are raised in response to a change in the value of a property. Often the property is a dependency property.

Why is my element losing focus during this event?

Because this event uses bubbling routing, the element that loses focus might be a child element instead of the element where the event handler is actually attached. Check the Source in the event data to determine the actual element that gained focus.


2 Answers

This is a common gotcha with TextBoxes in both WPF and WinForms. You can get around this by instructing the binding system to update the VM with every change to the TextBox instead of when it loses focus. To do this, set the UpdateSourceTrigger of the binding to PropertyChanged. This will write back to the VM any time the TextBox raises the PropertyChanged event for its Text property.

<TextBox Text="{Binding MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
like image 122
Michael Gunter Avatar answered Oct 12 '22 00:10

Michael Gunter


For the TextBox.Text dependency property, its default UpdateSourceTrigger is LostFocus (ie, your view model property gets updated when the control loses focus). To make the property update immediately whenever text is entered, set UpdateSourceTrigger=PropertyChanged. (See the link above for more info -- it actually covers your example specifically.)

like image 8
McGarnagle Avatar answered Oct 11 '22 22:10

McGarnagle