我有一个自制的“虚拟键盘”。用户可以通过键盘或“虚拟键盘”将文本插入TextBox
。如果他按下VirtualKey
,我会将Key
发送到TextBox
:
TextCompositionManager.StartComposition(new TextComposition(InputManager.Current, _TextBox, key));
如果他按下special Button
我通过EditingCommands
发送命令。但这只是部分工作。发送Backspace
有效,Tab
不起作用。 AcceptTab
设置为true。可以通过键盘插入tab
,只模拟此键不起作用。有人给我一个暗示吗?
case "Back":
EditingCommands.Backspace.Execute(null, _TextBox);
break;
case "Tab":
EditingCommands.TabForward.Execute(null, _TextBox);
break;
case "STab":
EditingCommands.TabBackward.Execute(null, _TextBox);
break;
case "Delete":
EditingCommands.Delete.Execute(null, _TextBox);
break;
Backspace
有效 TabForward
无效 TabBackward
无效 Delete
有效 XAML代码 HtVirtualKeyboardViewModel
<Style TargetType="Keyboard:HtVirtualKeyboardViewModel" x:Key="KeyboardLayoutTrimmed">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Keyboard:HtVirtualKeyboardViewModel">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="1" Name="KeyboardGrid" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Column="0" Padding="10" Background="{Extensions:HtThemeInfo Key=ChildWindow_Content_Background}" BorderBrush="{TemplateBinding BorderBrush}">
<ScrollViewer BorderThickness="0" MaxHeight="400" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<Grid Margin="-2">
<TextBox Name="tb1" Text="{Binding DisplayedText, RelativeSource={RelativeSource TemplatedParent}}" FontSize="{TemplateBinding FontSize}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" TextWrapping="Wrap" Style="{StaticResource KeyboardTextBox}"/>
</Grid>
</ScrollViewer>
</Border>
<ContentControl Grid.Row="1" ContentTemplate="{Binding KeyboardLayout, RelativeSource={RelativeSource TemplatedParent}}" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TextBox" x:Key="KeyboardTextBox">
<Setter Property="Background" Value="#D3D3D3"/>
<Setter Property="CaretBrush" Value="{Extensions:HtThemeInfo Key=ChildWindow_Border}"/>
<Setter Property="SelectionBrush" Value="{Extensions:HtThemeInfo Key=ChildWindow_Border}"/>
<Setter Property="AcceptsTab" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border
Name="Border"
Background="{TemplateBinding Background}">
<ScrollViewer Margin="2,4,2,4" x:Name="PART_ContentHost"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>