1
votes

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?

3
What is your goal? Why do you need to update the textbox when the window is loosing focus?unkreativ
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.user356178
If I'm not wrong, I should happen by default.Gopichandar
It is possible to write your own binding update behavior. Here's an example that updates source after a pause in typing 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
@Rachel I just learned that since WPF 4.5, it's now possible to do built-in. Check out my answer :)user356178

3 Answers

1
votes

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) ?

1
votes

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
votes

you may use custom behaviour class to achieve this functionality

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