【发布时间】: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 的文本输入有关的一些错误,但我不知道这是否可能是其中之一。