WPF:定制控制焦点管理

时间:2017-03-06 08:57:56

标签: focus wpf-controls

我想把重点放在用于构建自定义“Numeric Up Down”控件的TextBox上。 这是自定义的“Numeric Up Down”

<Style x:Key="SpinButton" TargetType="Slider" >
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="Slider">
    <Border Grid.RowSpan="3" BorderThickness="1,1,1,1" BorderBrush="WhiteSmoke">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".5*"/>
            <ColumnDefinition Width=".5*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height=".5*"/>
            <RowDefinition Height=".5*"/>
        </Grid.RowDefinitions>
        <TextBox Name="textBoxNumericUpDown" Grid.RowSpan="3" Margin="2,0,0,0"         Text="{Binding Value,Mode=TwoWay, RelativeSource={RelativeSource     TemplatedParent}}" FontSize="20" VerticalAlignment="Center" Background="    {x:Null}" Foreground="White" BorderThickness="0" CaretBrush="White"     IsTabStop="True"/>
        <RepeatButton Grid.Column="1" Grid.Row="0" Margin="0,0,-5,0"     Command="Slider.IncreaseLarge" Style="{StaticResource TimeRepeatButtonStyle}" Content="+" Background="{x:Null}" BorderBrush="White" Foreground="White"     FontSize="18" Cursor="Hand" Width="44" Height="44" IsTabStop="False"/>
        <RepeatButton Grid.Column="1" Grid.Row="1" Command="Slider.DecreaseLarge"     Margin="0,1,-5,0" Style="{StaticResource TimeRepeatButtonStyle}" Content="-"     Background="{x:Null}" BorderBrush="White" Foreground="White" FontSize="18"     Cursor="Hand" Width="44" Height="44" IsTabStop="False"/>
    </Grid>
    </Border>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
</Style>

用法

<Slider PreviewTextInput="textBoxTimeMinute_PreviewTextInput" Name="textBoxTimeMinute" Width="100" Style="{StaticResource SpinButton}" Maximum="999" Margin="8" HorizontalAlignment="Left" Minimum="0" IsTabStop="True" TabIndex="0"/>
<Label Grid.Column="1" Content=":" VerticalAlignment="Center" IsTabStop="False"/>
<Slider PreviewTextInput="textBoxTimeSecond_PreviewTextInput" Grid.Column="2" Name="textBoxTimeSecond" Width="100" Style="{StaticResource SpinButton}" Maximum="60" Margin="8" HorizontalAlignment="Left" Minimum="-1" IsTabStop="True" TabIndex="1"/>

Wanted result (Automatic focus on the first textbox if no number inside)

Wanted result (Automatic focus on the first textbox if number inside, number is selected)

我尝试了不同的方法来选择正确的Control,但它总是选择外层。在我的情况下网格/边框。 只有在按下TAB后,我才可以选择正确的TextBox控件。

WPF中是否有任何方法我可以在不按TAB的情况下实现对文本框的关注?

1 个答案:

答案 0 :(得分:1)

解决:

如果要将TextBox集中在ControlTemplate中,则必须在&#34; IsFocused &#34;上设置触发器。属性。 当它变为true时,您必须将 FocusManager.FocusedElement 字段设置为要聚焦的TextBox(或其他控件)的名称。

以下是我用来实现此目的的代码:

<ControlTemplate TargetType="Slider">
 <TextBox x:Name="TextBoxToFocus"/> //The textbox(or any other element) I want to set focus to. It is very important to have it named, otherwise I won't know which control to focus.
 <ControlTemplate.Triggers> //The trigger
      <Trigger Property = "IsFocused" Value = "True" /> //The property the trigger is watching
           <Setter TargetName="TextBoxToFocus" Property="FocusManager.FocusedElement" Value="{Binding ElementName=NumericUpDown}"/> //Setter when the trigger executes
      </Trigger>
 </ControlTelmplate.Triggers>