Tablet PC 输入面板不会引发 TextInput 事件?

发布于 2024-10-03 14:49:01 字数 2394 浏览 7 评论 0原文

我有一个使用 WPF TextInput 事件的自定义控件。使用键盘时效果很好;但是,如果使用“Tablet PC 输入面板”(Windows 7 自带)的手写识别,则单击“插入”按钮时不会发生 TextInput 事件。

public partial class Window1 : Window
{
    public Window1()
    {
    }

    protected override void OnTextInput(TextCompositionEventArgs e)
    {
        base.OnTextInput(e);
        this.Title = e.Text;
    }
}

class Text : Control
{
    static Text()
    {
        KeyboardNavigation.IsTabStopProperty.OverrideMetadata(
            typeof(Text), new FrameworkPropertyMetadata(true));
        KeyboardNavigation.TabNavigationProperty.OverrideMetadata(
            typeof(Text), new FrameworkPropertyMetadata(KeyboardNavigationMode.None));
        FocusableProperty.OverrideMetadata(
            typeof(Text), new FrameworkPropertyMetadata(true));
    }

    public static readonly DependencyProperty EnteredTextProperty =
        DependencyProperty.Register("EnteredText", typeof(string), typeof(Text),
                                    new FrameworkPropertyMetadata());

    public string EnteredText {
        get { return (string)GetValue(EnteredTextProperty); }
        set { SetValue(EnteredTextProperty, value); }
    }

    protected override void OnTextInput(TextCompositionEventArgs e)
    {
        this.EnteredText = e.Text;
        e.Handled = true;
    }

    /// <inheritdoc/>
    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        Focus();
    }
}

下面是 XAML:

<Window x:Class="TestProject.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300"
>
    <local:Text xmlns:local="clr-namespace:TestProject">
        <Control.Template>
            <ControlTemplate TargetType="local:Text">
                <Border Background="Beige">
                    <Viewbox>
                        <TextBlock Text="{TemplateBinding EnteredText}"/>
                    </Viewbox>
                </Border>
            </ControlTemplate>
        </Control.Template>
    </local:Text>
</Window>

当应用程序启动时,文本输入出现在标题栏中。这适用于键盘和手写识别。 一旦您在窗口内单击以将焦点授予控件,则只有键盘可以用于输入;手写识别被忽略。

有谁知道出了什么问题?为什么我在一种情况下得到 TextInput 事件,而在另一种情况下却得不到? 这是 WPF 中的错误吗?有解决办法吗?

I have a custom control that consumes WPF TextInput events. This works fine when using the keyboard; however, if the hand-writing recognition of the "Tablet PC Input Panel" (comes with Windows 7) is used, no TextInput event occurs when the "Insert" button is clicked.

public partial class Window1 : Window
{
    public Window1()
    {
    }

    protected override void OnTextInput(TextCompositionEventArgs e)
    {
        base.OnTextInput(e);
        this.Title = e.Text;
    }
}

class Text : Control
{
    static Text()
    {
        KeyboardNavigation.IsTabStopProperty.OverrideMetadata(
            typeof(Text), new FrameworkPropertyMetadata(true));
        KeyboardNavigation.TabNavigationProperty.OverrideMetadata(
            typeof(Text), new FrameworkPropertyMetadata(KeyboardNavigationMode.None));
        FocusableProperty.OverrideMetadata(
            typeof(Text), new FrameworkPropertyMetadata(true));
    }

    public static readonly DependencyProperty EnteredTextProperty =
        DependencyProperty.Register("EnteredText", typeof(string), typeof(Text),
                                    new FrameworkPropertyMetadata());

    public string EnteredText {
        get { return (string)GetValue(EnteredTextProperty); }
        set { SetValue(EnteredTextProperty, value); }
    }

    protected override void OnTextInput(TextCompositionEventArgs e)
    {
        this.EnteredText = e.Text;
        e.Handled = true;
    }

    /// <inheritdoc/>
    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        Focus();
    }
}

Here's the XAML:

<Window x:Class="TestProject.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300"
>
    <local:Text xmlns:local="clr-namespace:TestProject">
        <Control.Template>
            <ControlTemplate TargetType="local:Text">
                <Border Background="Beige">
                    <Viewbox>
                        <TextBlock Text="{TemplateBinding EnteredText}"/>
                    </Viewbox>
                </Border>
            </ControlTemplate>
        </Control.Template>
    </local:Text>
</Window>

When the app is started, text input appears in the title bar. This works both with keyboard and handwriting recognition.
Once you click within the window to give focus to the control, only the keyboard will work for input; the hand-writing recognition is ignored.

Does anyone know what is going wrong? Why do I get the TextInput event in one case but not in the other?
Is this a bug in WPF? Is there a work around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一江春梦 2024-10-10 14:49:01

插入后,控件将失去其焦点属性,并且如果您在 MouseDown 事件上调用 Focus(),则不会恢复它们(因为实际上它已获得焦点,并且 OnFocus 事件不会再次释放)。

如果您聚焦其他控件并将自定义控件再次聚焦,则会发生 TextInput 事件(直到您再次从平板电脑输入插入文本)。

我无法解释发生了什么,我认为这可能是 WPF 的错误,但也许该信息可以帮助解决问题......

The control is losing properties of its focus after the insertion and doesn't get them back if you call Focus() on MouseDown event (because actually it is focussed and the OnFocus event is not released again).

If you focus an other control and focus the custom control back its focussed again and the TextInput event occurs (until you insert text from the tablet pc input again).

I can't explain what is happing, I think that could be a bug of WPF but maybe that information could help for a work around...

风渺 2024-10-10 14:49:01

据我所知,当您单击窗口时,文本对象会失去焦点,因此不会捕获事件。

protected override void OnMouseDown(MouseButtonEventArgs e)
{
    base.OnMouseDown(e);
    Focus();
}

这似乎不起作用的原因是因为它需要单击文本对象来聚焦文本类

我的建议是:

  1. 为窗口的 mousedown 添加一个事件处理程序以聚焦文本项
  2. 当聚焦文本对象时 当在窗口中捕获按键时,您在窗口中键入 写入
  3. 文本对象和窗口标题

From what I can tell, when you click on the window, the text object loses focus and therefore will not capture events.

protected override void OnMouseDown(MouseButtonEventArgs e)
{
    base.OnMouseDown(e);
    Focus();
}

It seems the reason why this isn't working is because it requires the text object to be clicked to focus the text class

What I would suggest is:

  1. Add an event handler for mousedown of the window to focus the text item
  2. Focus the text object when you type in the window
  3. Write to both the text object and the window title when keys are captured in the window
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文