WPF:自定义控件焦点管理

WPF:自定义控件焦点管理

问题描述:

我正在将焦点集中在用于构建自定义“数字向上”控件的TextBox上。 这是自 “数字向上向下”WPF:自定义控件焦点管理

<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)

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

有没有什么办法可以在WPF中实现对文本框的关注而不用按TAB?

解决:

如果你想专注,你必须建立一个触发器在一个控件模板内一个TextBox“IsFocused”属性。 当它变为true时,您必须将FocusManager.FocusedElement字段设置为您要关注的文本框(或其他控件)的名称。

下面是我用来实现这一代码:

<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>