C# WPF自定义文本框选项卡索引聚焦
我有这种风格的文本框。当我使用它并尝试使用Tab按钮循环浏览我的内容时,使用这种样式的文本框在点击Tab按钮两次后获得焦点。但在第一次点击选项卡时,聚焦状态动画可以工作,但插入符号不存在。我再次点击tab,插入符号出现C# WPF自定义文本框选项卡索引聚焦,c#,wpf,textbox,focus,tabindex,C#,Wpf,Textbox,Focus,Tabindex,我有这种风格的文本框。当我使用它并尝试使用Tab按钮循环浏览我的内容时,使用这种样式的文本框在点击Tab按钮两次后获得焦点。但在第一次点击选项卡时,聚焦状态动画可以工作,但插入符号不存在。我再次点击tab,插入符号出现 <Style x:Key="MPTextBox" TargetType="{x:Type TextBox}"> <Setter Property="Template"> <Setter.Valu
<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>
我希望这一切只发生在一次点击标签上
这个文本框的另一个问题是,如果我试图访问事件处理程序中的文本属性,例如KeyDown事件,它将显示null,并且在文本框失去焦点之前,控件中的文本永远不会被设置。这里的问题是控件模板中存在文本框
如果控件模板中有event for TextBox,则可以知道控件模板的TextBox捕获的文本仅在lostFocus上,它将设置为应用样式的outerTextBoxwhere。如果要在外部文本中更新每个字符的文本,请在控件中指定代码UpdateSourceTrigger=PropertyChanged
模板的文本框。
默认情况下,仅在失去焦点时,控件模板文本框中的值在应用样式的外部文本框中更新
关于双焦点,这是因为边界和文本框
但下面是为文本框定义ControlTemplate的正确方法
因此,在setter中定义caretBrush和边框笔刷,如下所示
<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\u Windows\u Themes=clr名称空间:Microsoft.Windows.Themes;assembly=PresentationFramework.Classic这还会给我定制的外观吗?是的,我们的定制外观会保留下来。文本框的样式可以通过setter应用。文本框它有一个边框,因此通过setters设置边框样式。Statis是从windows开始的。抱歉,请将“s”替换为“s”。xmlns:x=并使用x:Static。自定义外观取决于适当的设置器。我在代码中只提到了两个setter作为示例。但是你试过第一点了吗。工作正常吗?对于每一次字符更改,您都能看到外部文本框的文本被更新吗?很抱歉,我会再试一次,并回答:D谢谢您的关心,您真棒