【问题标题】:WPF: Customized control focus managementWPF:自定义控件焦点管理
【发布时间】:2017-03-06 08:57:56
【问题描述】:

我想将焦点放在一个用于构建自定义“Numeric Up Down”控件的 TextBox 上。 这是自定义的“数字向上向下”

<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 的情况下实现对文本框的关注?

【问题讨论】:

    标签: focus wpf-controls


    【解决方案1】:

    已解决:

    如果您想在 ControlTemplate 中聚焦 TextBox,您必须在“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>
    

    【讨论】:

      猜你喜欢
      • 2012-10-22
      • 1970-01-01
      • 2023-03-13
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多