具有InputBindings的样式内的TextBox ControlTemplate

时间:2019-01-04 07:13:12

标签: wpf textbox styles controltemplate inputbinding

我已经在ControlTemplate内部为具有InputBindings(Enter的KeyBinding)的TextBox创建了WPF样式。 样式和InputBindings对于我的TextBoxes正常工作,但是如果我对TextBoxes使用此Style,则TabOrder / TabStop不再起作用。

这是样式:

<Style x:Key="TextBoxTemplate" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="OverridesDefaultStyle" Value="True" />                
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Margin" Value="5,0,5,5"/>
    <Setter Property="Width" Value="150"/>        

    <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="{x:Type TextBox}">
                 <Grid>  
                    <TextBox Text="{TemplateBinding Text}">
                         <TextBox.InputBindings>
                             <KeyBinding Command="{Binding EnterKeyCommand}" Key="Enter"/>
                         </TextBox.InputBindings>
                    </TextBox>
                 </Grid>
             </ControlTemplate>
        </Setter.Value>
     </Setter>
</Style>

如何将其添加到我的文本框:

<TextBox Text={Binding FirstName} Style="{StaticResource TextBoxTemplate}">
<TextBox Text={Binding LastName} Style="{StaticResource TextBoxTemplate}">

我认为问题是我在ControlTemplate中使用了TextBox。 但是我不知道如何在没有模板内部的TextBox的情况下运行InputBindings

你有什么主意吗? 谢谢菲尔

1 个答案:

答案 0 :(得分:2)

修改模板,使其看起来像原始模板以及您的KeyBinding

<Style x:Key="TextBoxTemplate" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Margin" Value="5,0,5,5"/>
    <Setter Property="Width" Value="150"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 
                                SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
                        <ScrollViewer.InputBindings>
                            <KeyBinding Command="{Binding EnterKeyCommand}" Key="Enter"/>
                        </ScrollViewer.InputBindings>
                    </ScrollViewer>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="true">
                        <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>