Suppose you had a text box bound to a property of a data item, and a button. If you enter text in the text box, then click on the button with the mouse, the following events happen in this order:
- The text is written from the control to the bound item
- The button click event is fired
However, if you activate the button with a mnemonic key, the text box does not lose focus. It seems that the text is written from the control to the bound item only when the text box loses focus.
Is there a known workaround to this? I want the same behaviour whether you left-click on the button, tab to the button and press space, or use the mnemonic.
I'll provide a complete example. If you type in the word "Hello" and press the button, you get a message box "WidgetName=Hello". But if you then changed it to "Goodbye" and press ALT-A, it will still say "WidgetName=Hello".
Here's the XAML code
<Window x:Class="BindingOrder.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindingOrder"
Title="Window1" Height="79" Width="282">
<Window.Resources>
<local:Widget x:Key="Widget" />
</Window.Resources>
<StackPanel Orientation="Horizontal" Height="30" VerticalAlignment="Top">
<TextBox
Width="200"
Margin="3, 3, 3, 3"
Text="{Binding Source={StaticResource Widget}, Path=WidgetName}" />
<Button
Click="OnApplyClicked"
Margin="3, 3, 3, 3">
_Apply
</Button>
</StackPanel>
</Window>
And the supporting code:
using System;
using System.Windows;
namespace BindingOrder
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void OnApplyClicked(object sender, RoutedEventArgs e)
{
Widget w = (Widget)this.Resources["Widget"];
MessageBox.Show(string.Format("WidgetName={0}", w.WidgetName));
}
}
public class Widget
{
public string WidgetName { get; set; }
}
}