1

A bit of context

<TextBox Text="{Binding Value, UpdateSourceTrigger=LostFocus}"/>

TextBox controls have a binding option called UpdateSourceTrigger, which is by default set to LostFocus. Howoever, this only works when the TextBox loses focus within its own Window. I require the binding to be updated even if the whole owner Window loses its focus.

Possible solution?

Now, the Window.Deactivated event seems to do this well.

<Window x:Class="MyWindow" Deactivated="OnDeactivated">

On to the question

That being said, how can the TextBox listen on its parent Window's Window.Deactivated event, considering that the TextBox lives under several layers of UserControl objects.

Window --> UserControl --> UserControl --> TextBox

How can I make the event bubble down from the Window to the TextBox? (ie: have the TextBox listen on the Window.Deactivated event)

Or maybe there's a better solution out there?

MaLio
  • 2,498
  • 16
  • 23
  • What is your goal? Why do you need to update the textbox when the window is loosing focus? – unkreativ Mar 16 '16 at 14:50
  • We need the value filled-in to be synced with other clients. If the user clicks elsewhere in the form, it's fine. But if he clicks in another window or app, the value doesn't get synced. Moreover, UpdateSourceTrigger=PropertyChanged is not a viable solution because it would trigger a sync each time the user types a new letter. –  Mar 16 '16 at 14:53
  • If I'm not wrong, I should happen by default. – Gopichandar Mar 16 '16 at 14:54
  • 1
    It is possible to write your own binding update behavior. [Here's an example that updates source after a pause in typing](http://stackoverflow.com/q/6711243/302677) that I have used in the past. Perhaps this could be used to write your own behavior, or maybe it will be suitable for you to use as it is. – Rachel Mar 16 '16 at 14:55
  • @Rachel I just learned that since WPF 4.5, it's now possible to do built-in. Check out my answer :) –  Mar 16 '16 at 18:31
  • @asmo Good to know! I haven't worked with WPF too much since 4.0, and I miss it. I'm falling behind on my WPF knowledge :) – Rachel Mar 16 '16 at 18:43

3 Answers3

1

Why not add a handler for the window deactivated event in your window class and then (within your handler) update your usercontrol (give it a Update method or something) ?

auburg
  • 1,373
  • 2
  • 12
  • 22
1

WPF 4.5 now supports Delay Binding. Combined with PropertyChanged, this means the value will be updated once the user finishes typing, but after a delay of X milliseconds. Pretty rad, right?

Text="{Binding Value, UpdateSourceTrigger=PropertyChanged, Delay=2000}"/>
0

you may use custom behaviour class to achieve this functionality

public class CustomBehavior : Behavior<UIElement>
{
//use the override the OnAttached()
}
Crispin
  • 25
  • 3