将密钥发送到TextBox仅部分工作

时间:2017-05-05 07:56:35

标签: c# wpf sendkeys

我有一个自制的“虚拟键盘”。用户可以通过键盘或“虚拟键盘”将文本插入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>

预览preview

1 个答案:

答案 0 :(得分:0)

问题是,当您点击Focus时,Button会丢失。

case "Tab":
    _TextBox.Focus();
    EditingCommands.TabForward.Execute(null, _TextBox);
    break;

再次关注TextBox,然后发送Command是解决方案!

工作解决方案

liveDemo