title: WPF Databind Before Saving
body: <p>In my WPF application, I have a number of databound TextBoxes. The <code>UpdateSourceTrigger</code> for these bindings is <code>LostFocus</code>. The object is saved using the File menu. The problem I have is that it is possible to enter a new value into a TextBox, select Save from the File menu, and never persist the new value (the one visible in the TextBox) because accessing the menu does not remove focus from the TextBox. How can I fix this? Is there some way to force all the controls in a page to databind?</p>
  
  <p><em>@palehorse: Good point. Unfortunately, I need to use LostFocus as my UpdateSourceTrigger in order to support the type of validation I want.</em></p>
  
  <p><em>@dmo: I had thought of that. It seems, however, like a really inelegant solution for a relatively simple problem. Also, it requires that there be some control on the page which is is always visible to receive the focus. My application is tabbed, however, so no such control readily presents itself.</em></p>
  
  <p><em>@Nidonocu: The fact that using the menu did not move focus from the TextBox confused me as well. That is, however, the behavior I am seeing. The following simple example demonstrates my problem:</em></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class="WpfApplication2.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300"&gt;
      &lt;Window.Resources&gt;
          &lt;ObjectDataProvider x:Key="MyItemProvider" /&gt;
      &lt;/Window.Resources&gt;
      &lt;DockPanel LastChildFill="True"&gt;
          &lt;Menu DockPanel.Dock="Top"&gt;
              &lt;MenuItem Header="File"&gt;
                  &lt;MenuItem Header="Save" Click="MenuItem_Click" /&gt;
              &lt;/MenuItem&gt;
          &lt;/Menu&gt;
          &lt;StackPanel DataContext="{Binding Source={StaticResource MyItemProvider}}"&gt;
              &lt;Label Content="Enter some text and then File &gt; Save:" /&gt;
              &lt;TextBox Text="{Binding ValueA}" /&gt;
              &lt;TextBox Text="{Binding ValueB}" /&gt;
          &lt;/StackPanel&gt;
      &lt;/DockPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <pre><code>using System;
  using System.Text;
  using System.Windows;
  using System.Windows.Data;
  
  namespace WpfApplication2
  {
      public partial class Window1 : Window
      {
          public MyItem Item
          {
              get { return (FindResource("MyItemProvider") as ObjectDataProvider).ObjectInstance as MyItem; }
              set { (FindResource("MyItemProvider") as ObjectDataProvider).ObjectInstance = value; }
          }
  
          public Window1()
          {
              InitializeComponent();
              Item = new MyItem();
          }
  
          private void MenuItem_Click(object sender, RoutedEventArgs e)
          {
              MessageBox.Show(string.Format("At the time of saving, the values in the TextBoxes are:\n'{0}'\nand\n'{1}'", Item.ValueA, Item.ValueB));
          }
      }
  
      public class MyItem
      {
          public string ValueA { get; set; }
          public string ValueB { get; set; }
      }
  }
  </code></pre>