Wpf 接受按钮和数据绑定不一致

Wpf 接受按钮和数据绑定不一致,wpf,data-binding,acceptbutton,Wpf,Data Binding,Acceptbutton,在表单上有一个“接受按钮”(WPF:IsDefault=“True”)是很方便的 在Windows窗体世界中,我曾经在按钮的相应单击事件中将数据从UI读取到对象 但是对于WPF,应该使用数据绑定。在窗口的构造函数中,我设置了this.DataContext=test 问题来了:用户在TextBox2中输入了一些文本,然后按Enter键。现在,绑定到OK按钮的命令被执行,数据被保存 但这不是正确的数据!为什么?TextBox2尚未失去焦点,因此ViewModel尚未更新。 将UpdateSourc

在表单上有一个“接受按钮”(WPF:IsDefault=“True”)是很方便的

在Windows窗体世界中,我曾经在按钮的相应单击事件中将数据从UI读取到对象

但是对于WPF,应该使用数据绑定。在窗口的构造函数中,我设置了this.DataContext=test

问题来了:用户在TextBox2中输入了一些文本,然后按Enter键。现在,绑定到OK按钮的命令被执行,数据被保存

但这不是正确的数据!为什么?TextBox2尚未失去焦点,因此ViewModel尚未更新。 将UpdateSourceTrigger更改为PropertyChanged并不总是合适的(例如,格式化的数字),我正在寻找一个通用的解决方案


如何克服这样的问题?

通常,当按下Enter键时,我会使用自定义的附加属性告诉WPF更新绑定源

它在XAML中的使用方式如下:

<TextBox Text="{Binding SomeProperty}" 
         local:TextBoxProperties.EnterUpdatesTextSource="True" />

也许您可以使用
TextBox2.GetBindingExpression(TextProperty).UpdateSource()显式更新数据
public class TextBoxProperties
{
    // When set to True, Enter Key will update Source
    public static readonly DependencyProperty EnterUpdatesTextSourceProperty =
        DependencyProperty.RegisterAttached("EnterUpdatesTextSource", typeof(bool),
                                            typeof(TextBoxProperties),
                                            new PropertyMetadata(false, EnterUpdatesTextSourcePropertyChanged));

    // Get
    public static bool GetEnterUpdatesTextSource(DependencyObject obj)
    {
        return (bool)obj.GetValue(EnterUpdatesTextSourceProperty);
    }

    // Set
    public static void SetEnterUpdatesTextSource(DependencyObject obj, bool value)
    {
        obj.SetValue(EnterUpdatesTextSourceProperty, value);
    }

    // Changed Event - Attach PreviewKeyDown handler
    private static void EnterUpdatesTextSourcePropertyChanged(DependencyObject obj,
                                                              DependencyPropertyChangedEventArgs e)
    {
        var sender = obj as UIElement;
        if (obj != null)
        {
            if ((bool)e.NewValue)
            {
                sender.PreviewKeyDown += OnPreviewKeyDownUpdateSourceIfEnter;
            }
            else
            {
                sender.PreviewKeyDown -= OnPreviewKeyDownUpdateSourceIfEnter;
            }
        }
    }

    // If key being pressed is the Enter key, and EnterUpdatesTextSource is set to true, then update source for Text property
    private static void OnPreviewKeyDownUpdateSourceIfEnter(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            if (GetEnterUpdatesTextSource((DependencyObject)sender))
            {
                var obj = sender as UIElement;
                BindingExpression textBinding = BindingOperations.GetBindingExpression(
                    obj, TextBox.TextProperty);

                if (textBinding != null)
                    textBinding.UpdateSource();
            }
        }
    }
}