Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set focus to UserControl (make it selectable)?

I need to set focus to UserControl itself, not its child.
Otherwise I cannot implement insertion from the buffer. :(
Setting Focusable=True doesn't help. Google and SO tells only how to set focus to UserControl child.

My control contains:
- Toolbar with several buttons bound to commands of the corresponding VM
- TextBox which is the input for the filter
- DataGrid - list of items.

I need to bind Ctrl+V command to VM. But to handle this gesture UserControl must have focus within. When there are no items in the grid (VM's collection is empty) buttons are disabled and the only element which can get focus is TextBox. But it handles Ctrl+V in its own way and I don't want to change this behavior.
Thus, I need something to set focus to when I click the area of UserControl.
I believe UserControl is the best candidate for it.
But I don't know how to make it selectable.

like image 271
Pavel Voronin Avatar asked Mar 22 '13 18:03

Pavel Voronin


1 Answers

The whole problem was in my misunderstanding of controls' behavior.
This SO question clearly shows it I believe.

Thus, setting UserControl.Focusable = true is not sufficient. To make it navigatable via keyboard IsTabStop must be true also. And to make UC selectable by mouse click we should call Focus() in mouse eventhandler. That's it.

like image 179
Pavel Voronin Avatar answered Oct 10 '22 12:10

Pavel Voronin