在退出事件之前发送 TextBox KeyEventArgs 值

Send TextBox KeyEventArgs value before exiting event

我有这个:

 private void AssociatedObject_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        if (textBox != null)
        {
            if (e.Key == Key.Return)
            {
                if (e.Key == Key.Enter)
                {
                    textBox.SelectAll();
                    e.Handled = true;
                }
            }
        }
    }

我希望能够:

  1. 将输入值发送到其绑定变量。
  2. 突出显示 TextBox 中的输入值。

此代码只高亮显示输入值,但不会将输入值发送到绑定变量。

做到了:

 private void AssociatedObject_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        if (textBox != null)
        {
            if (e.Key == Key.Return)
            {
                if (e.Key == Key.Enter)
                {
                    BindingExpression b = textBox.GetBindingExpression(TextBox.TextProperty);
                    if (b != null)
                    {
                        b.UpdateSource();
                    }

                    textBox.SelectAll();
                }
            }
        }
    }