Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Databind Before Saving

In my WPF application, I have a number of databound TextBoxes. The UpdateSourceTrigger for these bindings is LostFocus. 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?

@palehorse: Good point. Unfortunately, I need to use LostFocus as my UpdateSourceTrigger in order to support the type of validation I want.

@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.

@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:

<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">     <Window.Resources>         <ObjectDataProvider x:Key="MyItemProvider" />     </Window.Resources>     <DockPanel LastChildFill="True">         <Menu DockPanel.Dock="Top">             <MenuItem Header="File">                 <MenuItem Header="Save" Click="MenuItem_Click" />             </MenuItem>         </Menu>         <StackPanel DataContext="{Binding Source={StaticResource MyItemProvider}}">             <Label Content="Enter some text and then File > Save:" />             <TextBox Text="{Binding ValueA}" />             <TextBox Text="{Binding ValueB}" />         </StackPanel>     </DockPanel> </Window> 
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; }     } } 
like image 706
Joseph Sturtevant Avatar asked Sep 11 '08 20:09

Joseph Sturtevant


People also ask

How binding works in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

What is binding why binding of data is necessary?

Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.

What is binding source in WPF?

A binding source is usually a property on an object so you need to provide both the data source object and the data source property in your binding XAML. In the above example the ElementName attribute signifies that you want data from another element on the page and the Path signifies the appropriate property.

How to bind a property in XAML?

One-Way Data Binding The following XAML code creates four text blocks with some properties. Text properties of two text blocks are set to “Name” and “Title” statically, while the other two text blocks Text properties are bound to “Name” and “Title” which are class variables of Employee class which is shown below.


1 Answers

I found that removing the menu items that are scope depended from the FocusScope of the menu causes the textbox to lose focus correctly. I wouldn't think this applies to ALL items in Menu, but certainly for a save or validate action.

<Menu FocusManager.IsFocusScope="False" > 
like image 192
BigBlondeViking Avatar answered Oct 12 '22 03:10

BigBlondeViking