[英]Border thickness using style in WPF
I am making a little project, in which I am using data validation.我正在制作一个小项目,其中使用了数据验证。 Now I am trying to make styling for
TextBox
, when the input is not correct.现在,当输入不正确时,我正在尝试为
TextBox
制作样式。 I want to make a red border around the TextBox
and a ToolTip
with error message, which I return from C# code.我想在
TextBox
周围制作一个红色边框和一个带有错误消息的ToolTip
,这是我从 C# 代码返回的。
I started with making the red border.我开始制作红色边框。 I wrote this in XAML:
我用 XAML 写了这个:
<Window.Resources>
<Style x:Key="ErrorTemplate" TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="BorderBrush" Value="Red"></Setter>
<Setter Property="BorderThickness" Value="5"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
But if I write it like this, BorderThickness
property is changing inner border thickness of the TextBox
.但是如果我这样写,
BorderThickness
属性会改变TextBox
内边框厚度。 With the code above, I get this:使用上面的代码,我得到了这个:
Is there a way, to avoid this, and get something like this?有没有办法避免这种情况,并得到这样的东西?
And in a way, that I can add that ToolTip
, using the same Style
.在某种程度上,我可以使用相同的
Style
添加那个ToolTip
。
EDIT: Code of TextBox
:编辑:
TextBox
代码:
<TextBox
Grid.Row="1"
Grid.Column="3"
Margin="10px"
FontSize="14pt"
VerticalAlignment="Center"
Padding="5px"
Text="{Binding Path=Name, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Style="{StaticResource ErrorTemplate}">
<TextBox.DataContext>
<local:Data/>
</TextBox.DataContext>
</TextBox>
The blue border that you see is the border of the TextBox
itself, which is light blue by default in its keyboard focused state.您看到的蓝色边框是
TextBox
本身的边框,在键盘聚焦状态下默认为浅蓝色。 The border is 5 dips thick, because you explicitly set it in your provided ErrorTemplate
style.边框的厚度为 5 倾角,因为您在提供的
ErrorTemplate
样式中明确设置了它。 The red line around it is the default error template of the TextBox
.它周围的红线是
TextBox
的默认错误模板。
In order to meet your requirements, create a style like below.为了满足您的要求,请创建如下样式。
Margin
of 5 dips to account for the border that is displayed on error, it could otherwise be cut off outside the container since it is just an overlay over the original TextBox
and it is not resized.Margin
设置为 5 倾角以说明错误时显示的边框,否则它可能会在容器外被切断,因为它只是原始TextBox
的叠加层,并且不会调整大小。ErrorTemplate
, which is a special template dedicated to the error state, where AdornedElementPlaceholder
represents the original TextBox
in the template.ErrorTemplate
,这是一个专门用于错误状态的特殊模板,其中AdornedElementPlaceholder
代表模板中的原始TextBox
。<Style x:Key="MyTextBoxValidationStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Margin" Value="5"/>
<Setter Property="ToolTip" Value="There is no error."/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Border BorderBrush="Red" BorderThickness="5">
<AdornedElementPlaceholder/>
</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
Apply the style to your TextBox
using the Style
property.使用
Style
属性将Style
应用到您的TextBox
。
<TextBox Style="{StaticResource MyTextBoxValidationStyle}" ...>
Here is a sample screenshot of the result.这是结果的示例屏幕截图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.