WPF TextBox update binding when focus lost, or Enter key pressed, or click outside

I have a WPF TextBox, bound to a backing data variable, two way, set to update on FocusLost. It looks something like this:
<TextBox Name="xyzzy" Binding="{Binding MyData, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />

Open in new window


Issues:
1. I find myself pressing the Enter key when I'm finished editing the TextBox. I'd like the binding to update itself when I press the Enter key.

2. I'd also like the binding to update itself when I click outside of the TextBox, even if I click on something that isn't focusable. Currently clicking on something outside the TextBox that isn't focusable does not make the TextBox lose focus, so the binding isn't updated.
deleydSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

louisfrCommented:
1. Handle the TextBox's KeyDown event
2. Handle the Window's MouseDown event
Here's a sample window with associated code I made:
<Window x:Class="TopCoder.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TopCoder"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" MouseDown="Window_MouseDown">
    <StackPanel>
        <TextBox Name="xyzzy" Text="{Binding MyData, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Margin="0 50" KeyDown="xyzzy_KeyDown"/>
    </StackPanel>
</Window>

Open in new window

public partial class MainWindow : Window
{
    BindingExpression b;
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new Data();
        b = BindingOperations.GetBindingExpression(xyzzy, TextBox.TextProperty);
    }

    private void xyzzy_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter && b.IsDirty)
            b.UpdateSource();
    }

    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {

        if (b.IsDirty)
            b.UpdateSource();
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WPF

From novice to tech pro — start learning today.