In a WPF application, I have a control that I have derived from TextBox like this:
public class SelectableTextBlock : TextBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
e.Handled = false;
}
}
The OnKeyDown method is not called when entering a space into the TextBox, nor when hitting Backspace, but does fire for other input including normal printable characters (e.g. 'a') and modifier keys (e.g. ).
I'm using this control with IsReadOnly set to true so I can display selectable, uneditable text. The control used within WPFToolkit's DataGrid, and I want KeyDown events to propagate up the data grid, even if the SelectableTextBlock has focus, which is why I am using a custom control to explicitly mark the event as unhandled.
The problem is that the event isn't even making it to my control for certain keys. I can't just use OnPreviewKeyDown in the DataGrid to get round this, since I do want other controls used in the data grid to swallow the space KeyDown event.
Does anyone know how I can get the KeyDown event for the space key to propagate up?
Thanks.
The PreviewKeyDown event exists exactly for this sort of thing.
private void spacebarHandler_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
e.Handled = true;
}
Your KeyDown handler will never receive the KeyDown event for spacebar.
It seems the problem is that the space (and backspace etc.) key down event is being handled already within the TextBox, before it bubbles up to my derived control. I assume as part of the text composition process, as Wim posted.
To workaround this, I've added a handler that will receive the key down event even it has already been handled, and sets its Handled member to false, to allow it to carry on bubbling up normally. In the example below it just does this for space keys, but in my case I'll need to make it do this for any key events that I really don't want handled in my SelectedableTextBlock, as I don't know what key events parents might be interested in yet.
public class SelectableTextBlock : TextBox
{
public SelectableTextBlock() : base()
{
this.AddHandler(SelectableTextBlock.KeyDownEvent, new RoutedEventHandler(HandleHandledKeyDown), true);
}
public void HandleHandledKeyDown(object sender, RoutedEventArgs e)
{
KeyEventArgs ke = e as KeyEventArgs;
if (ke.Key == Key.Space)
{
ke.Handled = false;
}
}
...
}
I am of course still interested if anyone has a better solution...
Thanks, E.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With