保存前的 WPF 数据绑定 [英] WPF Databind Before Saving

查看:19
本文介绍了保存前的 WPF 数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 WPF 应用程序中,我有许多数据绑定文本框.这些绑定的 UpdateSourceTriggerLostFocus.使用文件菜单保存对象.我遇到的问题是,可以在 TextBox 中输入一个新值,从 File 菜单中选择 Save,并且永远不会保留新值(TextBox 中可见的值),因为访问菜单不会从 TextBox 中移除焦点.我怎样才能解决这个问题?有没有办法强制页面中的所有控件进行数据绑定?

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:好点.不幸的是,我需要使用 LostFocus 作为我的 UpdateSourceTrigger 以支持我想要的验证类型.

@dmo:我已经想到了.然而,对于一个相对简单的问题,这似乎是一个非常不雅的解决方案.此外,它还要求页面上有一些控件,该控件始终可见以接收焦点.但是,我的应用程序是选项卡式的,因此没有这样的控件可以轻易显示出来.

@Nidonocu:使用菜单并没有将焦点从 TextBox 移动这一事实也让我感到困惑.然而,这就是我所看到的行为.以下简单示例演示了我的问题:

<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:
'{0}'
and
'{1}'", Item.ValueA, Item.ValueB));
        }
    }

    public class MyItem
    {
        public string ValueA { get; set; }
        public string ValueB { get; set; }
    }
}

推荐答案

假设您在一个窗口中有一个 TextBox,并且其中有一个带有 Save 按钮的 ToolBar.假设 TextBox 的 Text 属性绑定到业务对象上的一个属性,并且绑定的 UpdateSourceTrigger 属性设置为 LostFocus 的默认值,这意味着当 TextBox 失去输入焦点时,绑定值会被推回业务对象属性.此外,假设 ToolBar 的 Save 按钮将其 Command 属性设置为 ApplicationCommands.Save 命令.

Suppose you have a TextBox in a window, and a ToolBar with a Save button in it. Assume the TextBox’s Text property is bound to a property on a business object, and the binding’s UpdateSourceTrigger property is set to the default value of LostFocus, meaning that the bound value is pushed back to the business object property when the TextBox loses input focus. Also, assume that the ToolBar’s Save button has its Command property set to ApplicationCommands.Save command.

在这种情况下,如果您编辑 TextBox 并用鼠标单击 Save 按钮,就会出现问题.单击 ToolBar 中的 Button 时,TextBox 不会失去焦点.由于不会触发 TextBox 的 LostFocus 事件,因此 Text 属性绑定不会更新业务对象的源属性.

In that situation, if you edit the TextBox and click the Save button with the mouse, there is a problem. When clicking on a Button in a ToolBar, the TextBox does not lose focus. Since the TextBox’s LostFocus event does not fire, the Text property binding does not update the source property of the business object.

显然,如果 UI 中最近编辑的值尚未推送到对象中,则不应验证和保存对象.这正是 Karl 解决的问题,通过在他的窗口中编写代码手动查找具有焦点的 TextBox 并更新数据绑定的源.他的解决方案工作得很好,但它让我想到了一个通用的解决方案,它在这个特定场景之外也很有用.进入命令组...

Obviously you should not validate and save an object if the most recently edited value in the UI has not yet been pushed into the object. This is the exact problem Karl had worked around, by writing code in his window that manually looked for a TextBox with focus and updated the source of the data binding. His solution worked fine, but it got me thinking about a generic solution that would also be useful outside of this particular scenario. Enter CommandGroup…

摘自 Josh Smith 关于 CommandGroup

Taken from Josh Smith’s CodeProject article about CommandGroup

这篇关于保存前的 WPF 数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆