WPF:的onkeydown()不会被调用从WPF的TextBox派生控制空格键 [英] WPF: OnKeyDown() not being called for space key in control derived from WPF TextBox

查看:1185
本文介绍了WPF:的onkeydown()不会被调用从WPF的TextBox派生控制空格键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个WPF应用程序,我有我从文本框派生这样的控件:

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;
    }
}



中,onKeyDown方法不是进入一个空间分成调用时文本框,也没有打Backspace键时,但确实火了其他输入包括正常打印的字符(如A)和修饰键(例如)。

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. ).

我使用这种控制并且通过isReadOnly设置为true,这样我就可以显示可选择的,不可编辑的文本。 WPFToolkit的数据网格中使用的控制,我想的KeyDown事件传播了数据网格,即使SelectableTextBlock具有焦点,这就是为什么我使用一个自定义的控制,明确标示该事件为未处理。

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.

的问题是,该事件是不是甚至它使我的控制为一定的键。我不能只使用OnPreviewKeyDown在DataGrid来避开这一点,因为我想在数据网格用于吞下空间KeyDown事件等控制。

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.

有谁知道我怎么能得到KeyDown事件对于空格键向上传播?

Does anyone know how I can get the KeyDown event for the space key to propagate up?

感谢。

推荐答案

看来问题是,空间(和退格等)键按下事件正在处理已经在TextBox内,堆满了我的派生的控件之前。我想作为文本组合过程的一部分,维姆公布。

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.

要解决此,我补充说,甚至会收到key down事件已先后处理程序被处理,并设置其处理的成员为false,以使其能够进行正常冒泡。在它下面的例子只是做这行空格键,但对我来说,我需要使它成为我的任意键事件做到这一点真的的不希望在我的SelectedableTextBlock处理,因为我。不知道是什么重要事件的父母可能会感兴趣的又

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...

谢谢,
é。

这篇关于WPF:的onkeydown()不会被调用从WPF的TextBox派生控制空格键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆