简体   繁体   English

在 WPF 中使用样式的边框粗细

[英]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.为了满足您的要求,请创建如下样式。

  • Set a general 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的叠加层,并且不会调整大小。
  • Set an optional default tool tip for the non-error state.为非错误状态设置可选的默认工具提示。
  • Set an ErrorTemplate , which is a special template dedicated to the error state, where AdornedElementPlaceholder represents the original TextBox in the template.设置一个ErrorTemplate ,这是一个专门用于错误状态的特殊模板,其中AdornedElementPlaceholder代表模板中的原始TextBox
  • Set a trigger that changes the tool tip text to the validation error in error state.设置一个触发器,将工具提示文本更改为错误状态下的验证错误。
<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM