C# 退出事件前发送文本框KeyEventArgs值

C# 退出事件前发送文本框KeyEventArgs值,c#,wpf,keyeventargs,C#,Wpf,Keyeventargs,我有这个: private void AssociatedObject_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { TextBox textBox = sender as TextBox; if (textBox != null) { if (e.Key == Key.Return) {

我有这个:

 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;
                }
            }
        }
    }
我希望能够:

  • 将输入值发送到其绑定变量
  • 突出显示
    文本框中的输入值
  • 此代码仅突出显示输入值,但不将输入值发送到绑定变量。

    这样做:

     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();
                    }
                }
            }
        }
    

    嗯,在那个片段中很难看到“绑定变量”。考虑将焦点更改为另一个控件。看到绑定的变量片段不会帮助解决这个问题。绑定的变量就是textbox.text。更改焦点不起作用,因为我想将焦点保持在文本框上,以便突出显示文本。谢谢。我知道,但愿我们不会得到一个我们可以自己运行的片段。演示如何更好地提出SO问题。您提供的问题显示了如何捕获和输入关键事件。我已经这样做了,如上面的代码所示。我希望能够在突出显示输入值的同时传递输入值。目前,使用我提供的代码,我只能突出显示输入值。但我无法在突出显示输入值后发送输入值。