WPF自定义文本框选项卡索引聚焦



我有这种风格的文本框。当我使用它并尝试使用Tab按钮循环浏览我的内容时,使用这种风格的文本框在点击Tab按钮两次后会获得焦点。不过,在第一个Tab键中,"聚焦"状态动画可以工作,但插入符号不在那里。我再次点击制表符,插入符号出现了。

<Style x:Key="MPTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border" BorderBrush="#FFEF7B54" BorderThickness="2" Background="White" CornerRadius="5">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled"/>
<VisualState x:Name="ReadOnly"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FF57C0AF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Unfocused"/>
<VisualState x:Name="Focused">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="#FFED4B15"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBox Background="{x:Null}" BorderBrush="{x:Null}" CaretBrush="#FFF05A29" BorderThickness="0" Margin="5,5,5,0" FontFamily="Public Enemy NF" FontSize="16" Foreground="#FFF05A29" HorizontalAlignment="Stretch" d:LayoutOverrides="Width, Height" VerticalAlignment="Stretch" Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

我希望这一切只发生在一个标签点击。

这个文本框的另一件事是,如果我试图访问事件处理程序中的Text属性,例如KeyDown事件,它显示为null,并且控件中的文本永远不会设置,直到文本框失去焦点。

这里的问题是控件模板中存在文本框

  1. 如果您在控件模板中有TextBox的事件,您可以知道文本是由控件模板的文本框捕获的,并且只有在lostFocs上,它才会设置为outerTextBox(应用样式的位置)。如果希望每个字符的文本都在外部文本中更新,则在控件中指定代码UpdateSourceTrigger=PropertyChanged模板的文本框。默认情况下,只有在失去焦点时,控件Template的文本框中的值才会在外部TextBox(应用样式的位置)中更新。

  2. 关于双焦点,这是因为边界和文本框。

但是以下是为textBox定义ControlTemplate的正确方法因此,在setter中定义carteBrush、Border brush如下

<Style x:Key="MPTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Microsoft_Windows_Themes:ClassicBorderDecorator x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" BorderStyle="Sunken" Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled"/>
<VisualState x:Name="ReadOnly"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FF57C0AF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Unfocused"/>
<VisualState x:Name="Focused">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="#FFED4B15"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ScrollViewer x:Name="border" FontWeight="Bold">
<ScrollViewer.Background>
<!-- Button: common to all buttons -->
<SolidColorBrush Color="{DynamicResource VeryLightGray}"/>
</ScrollViewer.Background>
</ScrollViewer>                            
</Microsoft_Windows_Themes:ClassicBorderDecorator>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="CaretBrush" Value="{DynamicResource {x:static SystemColors.WindowTextBrushKey}}"/>

此处xmlns:Microsoft_Windows_Themes="clr命名空间:Microsoft.Windows.Themes;assembly=PresentationFramework.Classic">

最新更新