WPF:从 WPF 文本框派生的控件中没有为空格键调用 OnKeyDown() [英] WPF: OnKeyDown() not being called for space key in control derived from WPF TextBox

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

问题描述

在 WPF 应用程序中,我有一个从 TextBox 派生的控件,如下所示:

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

在 TextBox 中输入空格或按 Backspace 时不会调用 OnKeyDown 方法,但会触发其他输入,包括正常的可打印字符(例如 '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 的 DataGrid 中使用的控件,我希望 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.

问题是事件甚至没有让我控制某些键.我不能只使用 DataGrid 中的 OnPreviewKeyDown 来解决这个问题,因为我确实希望数据网格中使用的其他控件能够吞下空间 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 中处理,然后它会冒泡到我的派生控件.正如 Wim 所发布的,我认为这是文本撰写过程的一部分.

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.

为了解决这个问题,我添加了一个处理程序,即使它已经被处理,它也会接收按键按下事件,并将其 Handled 成员设置为 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...

谢谢,E.

这篇关于WPF:从 WPF 文本框派生的控件中没有为空格键调用 OnKeyDown()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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