当焦点在复选框/处理previewTextInput在主窗口中,而不是让孩子控制偷字符previewTextInput不会触发 [英] PreviewTextInput is not fired when the focus is on checkbox / Handling the PreviewTextInput in Main window and not allowing child control steal the characters

查看:355
本文介绍了当焦点在复选框/处理previewTextInput在主窗口中,而不是让孩子控制偷字符previewTextInput不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code。该窗口有一个文本框和一个复选框。如果我有重点以外的任何其他复选框,然后键入类似123-456然后为每个字符previewKeyDown和previewTextInput是射击。

但是,如果我有重点复选框,然后键入123-456则previewKeyDown被解雇,而previewTextInput火灾中的所有字符只为123456,并且不火 - 。连字符正在由复选框处理,并且不获取传递到previewTextInput。有没有办法让连字符,以previewTextInput?

 公共窗口1()
        {
            的InitializeComponent();

            TextCompositionManager.AddTextInputHandler(这一点,新TextCompositionEventHandler(Window_ previewTextInput));
        }

私人无效Window_ previewKeyDown(对象发件人,System.Windows.Input.KeyEventArgs E)
        {

        }

私人无效Window_ previewTextInput(对象发件人,TextCompositionEventArgs E)
        {
}
 

解决方案

我发现了一个办法做到这一点,但我想从专家知道是否有与我的解决方案一个问题或一个更好的方式来做到这一点。

在窗口的KeyDown事件,我标志着处理,以虚假的。该复选框,设置处理,以真正在KeyDown和在窗口的的KeyDown我将它设置为false,这将调用previewTextInput作为事件仍需要处理。

 公共窗口1()
        {
            的InitializeComponent();
            TextCompositionManager.Add previewTextInputStartHandler(这一点,新TextCompositionEventHandler(Window_ previewTextInput));
            this.AddHandler(Window.KeyDownEvent,新System.Windows.Input.KeyEventHandler(Window_KeyDown),TRUE);
        }

私人无效Window_ previewTextInput(对象发件人,TextCompositionEventArgs E)
        {
        }

私人无效Window_KeyDown(对象发件人,System.Windows.Input.KeyEventArgs E)
        {
            e.Handled = FALSE;
        }
 

I have the following code. The window has a textbox and a checkbox. If I have focus on the anything other than checkbox and type something like 123-456 then for each character PreviewKeyDown and PreviewTextInput are firing.

But if I have the focus to checkbox and then type 123-456 then the PreviewKeyDown is fired for all the characters whereas PreviewTextInput fires only for 123456 and doesn't fire for '-'. The hyphen is being handled by the checkbox and not getting passed to PreviewTextInput. Is there a way to get the hyphen to PreviewTextInput?

 public Window1()
        {
            InitializeComponent();

            TextCompositionManager.AddTextInputHandler(this, new TextCompositionEventHandler(Window_PreviewTextInput));       
        }

private void Window_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
}

解决方案

I found a way to do this but I want to know from experts if there is a problem with my solution or a better way to do this.

In the KeyDown event of the window I am marking the Handled to false. The checkbox sets the Handled to true in the KeyDown and in the Window's KeyDown I am setting it to false and this will call the PreviewTextInput as the event still needs to be handled.

public Window1()
        {
            InitializeComponent();
            TextCompositionManager.AddPreviewTextInputStartHandler(this, new TextCompositionEventHandler(Window_PreviewTextInput));
            this.AddHandler(Window.KeyDownEvent, new System.Windows.Input.KeyEventHandler(Window_KeyDown), true);
        }

private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
        }

private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            e.Handled = false;
        }

这篇关于当焦点在复选框/处理previewTextInput在主窗口中,而不是让孩子控制偷字符previewTextInput不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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