选择 WPF 文本框边框时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3832085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF TextBox Border when selected?
提问by MadSeb
I want to make a WPF TextBox have a DarkBlue border and thickness equal to 1. I want to make the WPF have this border ( DarkBlue, thickness set to 1 ) even when the TextBox is selected.
我想让 WPF 文本框具有深蓝色边框和等于 1 的厚度。即使选择了文本框,我也想让 WPF 具有此边框(深蓝色,厚度设置为 1 )。
I tried doing this task by the following code. However, it doesn't work at all. Any ideas or hints ? Any help would be greatly appreciated.
我尝试通过以下代码完成此任务。但是,它根本不起作用。任何想法或提示?任何帮助将不胜感激。
<Style x:Key="ReadOnlyLargeTextBox" TargetType="{x:Type TextBox}" >
<Setter Property="Height" Value="80"/>
<Setter Property="MaxHeight" Value="80"/>
<Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
<Style.Triggers>
<Trigger Property="TextBox.IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="DarkBlue"/>
<Setter Property="BorderThickness" Value="1"/>
</Trigger>
<Trigger Property="TextBox.IsMouseOver" Value="False">
<Setter Property="BorderBrush" Value="DarkBlue"/>
<Setter Property="BorderThickness" Value="1"/>
</Trigger>
</Style.Triggers>
</Style>
P.SNote that the text box does not have an IsSelected property.
PS注意文本框没有 IsSelected 属性。
回答by Kishore Kumar
just see is this you want...
看看这是你想要的...
<Style x:Key="TextBoxStyle1" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
<Setter Property="BorderThickness" Value="1"/>
<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="DarkBlue"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
回答by Chris Persichetti
I think your problem is due to having the Trigger Property value containing TextBox. You just need the name of the property.
我认为您的问题是由于 Trigger Property 值包含TextBox。您只需要属性的名称。
<Style x:Key="ReadOnlyLargeTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="80"/>
<Setter Property="MaxHeight" Value="80"/>
<Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="Blue"/>
<Setter Property="BorderThickness" Value="1"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Blue"/>
<Setter Property="BorderThickness" Value="1"/>
</Trigger>
</Style.Triggers>
</Style>
回答by Matěj Zábsky
Check FocusVisualStyleproperty of the FrameworkElement object (ancestor of TextBox). It's purpose is to define style applied when an element is selected.
检查FrameworkElement 对象(TextBox 的祖先)的FocusVisualStyle属性。它的目的是定义选择元素时应用的样式。
回答by ozczecho
You have the same logic for when "IsMouseOver" True as well False. Change one and you should see something.
当“IsMouseOver”为 True 和 False 时,您有相同的逻辑。换一个,你应该会看到一些东西。