0

我有这个:

 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

此代码仅突出显示输入值,但不会将输入值发送到绑定变量。

4

1 回答 1

0

这做到了:

 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();
                }
            }
        }
    }
于 2018-05-23T23:10:29.943 回答