2017-03-06 1 views
0

사용자 지정 "Numeric Up Down"컨트롤을 작성하는 데 사용되는 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)

사용하지만, 항상 바깥 쪽 레이어를 선택합니다. 내 경우에는 Grid/Border. Tab 키를 누른 후에 만 ​​올바른 TextBox 컨트롤을 선택할 수 있습니다.

WPF에서 Tab을 누르지 않고 텍스트 상자에 중점을 둘 수있는 방법이 있습니까?

답변

1

는 해결 :

당신이에 트리거를 설정하는 가지고 ControlTemplate이 내부에 텍스트 상자를 집중하려면

재산 " 을 IsFocused". 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> 

: 아래

내가 이것을 달성하는 데 사용되는 코드입니다
관련 문제