XAML 文本框在焦点上更改边框颜色 [英] XAML textbox change border color on focus
本文介绍了XAML 文本框在焦点上更改边框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前遇到的问题是,在IsFocused"属性上,我可以触发文本框的背景更改,但是当我想更改 BorderBrush 时它不起作用.
The issue I am currently having is that on the 'IsFocused' property I am able to Trigger the background for the textbox to change but when I want to change the BorderBrush it does not work.
<TextBox Padding="2" FontFamily="Sans Serif" Foreground="Red" FontSize="10px" FontWeight="Medium" Width="200" BorderThickness="2" VerticalAlignment="Center">
<TextBox.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="2"/>
</Style>
</TextBox.Resources>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="#858585"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
谁能看出这行不通的原因?
Can anyone see a reason why this would not work?
我刚刚发现当我右键单击"时它会变成红色?当用户点击文本框时,我希望它变成红色.
I have just discovered that when I 'Right-Click' it changes to Red? I want it to change to red when the user clicks on the textbox.
推荐答案
尝试使用该代码
<TextBox
Padding="2" FontFamily="Sans Serif"
Foreground="Red" FontSize="10px"
FontWeight="Medium" Width="200"
VerticalAlignment="Center">
<TextBox.Style>
<Style BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="bg" BorderBrush="#FF825E5E" BorderThickness="1">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="Red"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="Red"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
<Trigger Property="IsFocused" Value="False">
<Setter Property="BorderBrush" TargetName="bg" Value="#858585"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="BorderBrush" TargetName="bg" Value="#858585"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TextBox.Style>
</TextBox>
这篇关于XAML 文本框在焦点上更改边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文