Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do mouse clicks bring keyboard focus to focusable controls by default?

Tags:

focus

wpf

This question can seem quite odd but from all my experience I've gotten used to setting keyboard focus to the focusable element just by clicking it with the mouse; however, UserControl with the properties Focusable = true and IsTabStop = true came as a surprise to me because it gets keyboard focus via Tab but stays ignorant to mouse clicks.

like image 324
Pavel Voronin Avatar asked Mar 24 '13 15:03

Pavel Voronin


People also ask

How do I make my keyboard focus?

Understanding Focus When an HTML element is able to handle keyboard input, it is said to have focus. Exactly one element is able to have focus in a time. In most browsers, users can move focus by pressing the Tab key and the Shift + Tab keys.

What is meant by keyboard focusable?

Keyboard focus refers to the element that is currently receiving keyboard input. There can be only one element on the whole desktop that has keyboard focus. In WPF, the element that has keyboard focus will have IsKeyboardFocused set to true .


1 Answers

Handle the click event on the UserControl and add this code to the eventhandler:

private void UserControl_OnMouseUp(object sender, MouseButtonEventArgs e)
{
   Keyboard.Focus(sender as UserControl);
}
like image 57
Ralf de Kleine Avatar answered Oct 09 '22 08:10

Ralf de Kleine