9
votes

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.

1
Otherwise I cannot implement insertion from the buffer - Can you please explain this better? your question is too vague dude, I can't understand. Also post your current code and XAML.Federico Berasategui
I've added some details. Hope it is clearer now.Pavel Voronin

1 Answers

23
votes

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.