Tablet PC 输入面板不会引发 TextInput 事件?
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
插入后,控件将失去其焦点属性,并且如果您在 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...
据我所知,当您单击窗口时,文本对象会失去焦点,因此不会捕获事件。
这似乎不起作用的原因是因为它需要单击文本对象来聚焦文本类
我的建议是:
From what I can tell, when you click on the window, the text object loses focus and therefore will not capture events.
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: