【问题标题】:Tablet PC Input Panel does not cause TextInput event?平板电脑输入面板不会引起TextInput事件?
【发布时间】:2011-05-12 07:29:07
【问题描述】:

我有一个使用 WPF TextInput 事件的自定义控件。这在使用键盘时效果很好;但是,如果使用“Tablet PC Input Panel”(Windows 7自带)的手写识别,点击“Insert”按钮时不会发生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 中的错误吗?有解决办法吗?

【问题讨论】:

  • 您使用的是哪个版本的 .NET?我知道他们修复了 .NET4 中与 WPF 的文本输入有关的一些错误,但我不知道这是否可能是其中之一。

标签: c# wpf textinput


【解决方案1】:

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

如果您聚焦另一个控件并将自定义控件重新聚焦到其聚焦的位置,并且 TextInput 事件发生(直到您再次从平板电脑输入插入文本)。

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

【讨论】:

    【解决方案2】:

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

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

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

    我的建议是:

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

    【讨论】:

    • TextInput 在使用键盘时工作正常,因此键盘焦点在 Text 对象上。只有手写识别失败。看来手写识别可能有一个单独的焦点??
    猜你喜欢
    • 2012-02-14
    • 2014-06-25
    • 1970-01-01
    • 2013-06-14
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2011-12-14
    相关资源
    最近更新 更多