How can I set focus on a TextBox
when F3
key pressed.
In other words, do some thing like bellow:
private void Window_PreviewKeyDown(object sender, KeyEventArgs )
{
switch (e.Key)
{
case Key.F3:
myTextBox1.Focus();
break;
case Key.F4:
myTextBox2.Focus();
break;
default:
break;
}
}
note:I want to do it in xaml.
You could do this by created an attached property that takes a shortcut key then create an input binding on the window hosting that control.. it's a bit of a complex class but very easy to use.
start by adding a new class to your project below.
now in XAML all you need to do to set your focus keys is assign that FocusKey property, an example below has 2 textboxes, one gets focus when F1 is pressed, the other when F7 is pressed.