0

I have this:

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

I want to be able to:

  1. Send the input value to its bound variable.
  2. Highlight the input value in TextBox.

This code only highlights the input value but does not send the input value to the bound variable.

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
JP Garza
  • 212
  • 3
  • 16
  • Hmm, "binded variable" is hard to see in that snippet. Consider to also change the focus to another control. – Hans Passant May 23 '18 at 22:39
  • Seeing the binded variable snippet would not help solve the issue. The binded variable is just the textbox.text. Changing Focus does not work because I want to keep focus on the textbos so I can highlight the text. Thank you. – JP Garza May 23 '18 at 22:42
  • I know, god forbid we'd get a snippet we can run ourselves. [This question](https://stackoverflow.com/questions/5556489/capturing-the-enter-key-in-a-textbox) shows how to do a better job of asking an SO question. And surely is a duplicate of this one. – Hans Passant May 23 '18 at 22:51
  • The question you provided shows how to capture and enter key event. I have already done that as shown on the code above. I want to be able to pass the input value while still highlighting the input value. Currently, with the code I have provided, I can only highlight the input value. But I cannot send the input value while highlighting the input value after. – JP Garza May 23 '18 at 22:58

1 Answers1

0

This did it:

 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();
                }
            }
        }
    }
JP Garza
  • 212
  • 3
  • 16