First: I know that there are literally thousands of answers like: "Add a handler to Keyboard.KeyDownEvent
and have fun!". But in my situation this does not work.
I have a custom control CustomControl
which derives from Canvas
but has no Children
. Instead it draws its "children" directly to the DrawingContext
in the OnRender
. My Control is HitTestVisible, it is tab stop but is not focusable. It is often reused and sometimes in a ScrollViewer
.
This CustomControl
has a custom implementation for selecting something like text, and should copy that selected text to the ClipBoard on Ctrl+C.
To do this, I added a handler in the constructor:
public CustomControl()
{
//// ... other stuff
AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)CopyMarkedNucleotidesToClipboard);
}
And here is the Problem: When my control is inside a ScrollViewer
, and I hit Ctrl+C, the KeyDownEvent
is raised on the ScrollViewer
and bubbles up to the window, and therefore never reaches my Control.
What can I do inside my CustomControl
to capture every Ctrl+C in the window where it resides?
PS: I already set IsTabStop="False"
and Focusable="False"
. But then the next sibling of the ScrollViewer
would raise the event which would still bubble up to the window. And I don't want to go through all controls which are higher in the visual tree and set IsTabStop="False"
and Focusable="False"
which would be wrong...
I already found this article http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx but I think, that there must be a more wpf-like way!