WPF 扩展器在缩小时仍然显示验证错误装饰器

发布于 2024-08-05 03:44:49 字数 1921 浏览 5 评论 0原文

我有一个 TextBox 的样式来显示验证错误消息,如下所示:

<Style TargetType="{x:Type TextBox}">
       <Style.Triggers>
           <Trigger Property="Validation.HasError" Value="true">
               <Setter Property="ToolTip"
                       Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                       Path=(Validation.Errors)[0].ErrorContent}"/>
           </Trigger>
       </Style.Triggers>
       <Setter Property="Validation.ErrorTemplate">
           <Setter.Value>
               <ControlTemplate>
                   <StackPanel Orientation="Horizontal">
                       <Border BorderBrush="{Binding Path=ErrorContent, 
                               Converter={StaticResource ValidationErrorToBrushConverter}}" BorderThickness="2">
                           <AdornedElementPlaceholder />
                       </Border>
                       <Image Name="image1" Height="14" Width="14" Stretch="Fill" Margin="1,1,1,1" 
                              Source="{Binding Path=ErrorContent, 
                              Converter={StaticResource ValidationErrorToImageSourceConverter}}" 
                              ToolTip="{Binding Path=ErrorContent}"/>
                   </StackPanel>
               </ControlTemplate>
           </Setter.Value>
       </Setter>
   </Style>

TextBox 位于 Expander 中。当我打开 Expander 时,TextBox 允许输入,但如果输入为 NullorEmpty 或包含特殊字符,验证将失败。

我的问题是,当我触发验证错误时,TextBox 会呈红色亮起,并显示一个带有消息的图标作为工具提示。到目前为止一切都很好。但是当我关闭 Expander 而不通过验证时,红色轮廓和带有工具提示的图标仍然存在!即使 Expander 缩小了!只是漂浮在那里......这不是好行为。

关于如何将验证内容与 Expander 中的所有其他控件一起隐藏的任何想法?此外,验证样式是在 UserControl 的资源中声明的,而不是在 Expander 本身中声明的。

I've got a style for a TextBox to show a validation error message as follows:

<Style TargetType="{x:Type TextBox}">
       <Style.Triggers>
           <Trigger Property="Validation.HasError" Value="true">
               <Setter Property="ToolTip"
                       Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                       Path=(Validation.Errors)[0].ErrorContent}"/>
           </Trigger>
       </Style.Triggers>
       <Setter Property="Validation.ErrorTemplate">
           <Setter.Value>
               <ControlTemplate>
                   <StackPanel Orientation="Horizontal">
                       <Border BorderBrush="{Binding Path=ErrorContent, 
                               Converter={StaticResource ValidationErrorToBrushConverter}}" BorderThickness="2">
                           <AdornedElementPlaceholder />
                       </Border>
                       <Image Name="image1" Height="14" Width="14" Stretch="Fill" Margin="1,1,1,1" 
                              Source="{Binding Path=ErrorContent, 
                              Converter={StaticResource ValidationErrorToImageSourceConverter}}" 
                              ToolTip="{Binding Path=ErrorContent}"/>
                   </StackPanel>
               </ControlTemplate>
           </Setter.Value>
       </Setter>
   </Style>

The TextBox lives in an Expander. When I open the Expander, the TextBox allows for input, but will fail validation if the input is NullorEmpty, or contains special characters.

My problem is that when I trigger a validation error, the TextBox lights up in red and shows an icon with the message as a tooltip. All good so far. BUT when i close the Expander without passing validation, the red outline and icon with tooltip are still there! Even with the Expander shrunk down! Just floating there... This is not good behavior.

Any ideas on how to get the Validation stuff to hide along with all the other controls in the Expander? Also, the Style for validation is declared in the resources of the UserControl, not in the Expander itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕巷 2024-08-12 03:44:49

我最终只是在关闭扩展器时清除了文本框。这样,验证错误就会消失,并且当扩展器重新打开时,该框会变得清晰并准备好进行另一个输入。

I ended up simply clearing the TextBox upon closing the Expander. That way, the validation error goes away and the box is clear and ready for another input when the Expander is opened back up.

吝吻 2024-08-12 03:44:49

我也有同样的问题。我通过将 AdornerDecorator 作为扩展器的第一个子对象来修复它。当 Expander 折叠时,AdornerDecorator 也会折叠,因此 Adorner 也应该全部消失。

I had the same problem. I fixed it by putting an AdornerDecorator as the first child object of the expander. The AdornerDecorator is collapsed when the Expander is collapsed, so the Adorners should all disappear too.

初见你 2024-08-12 03:44:49

我通过在隐藏 TextBox 时将 Validation.ErrorTemplate 属性设置为 null 解决了同样的问题

<Style TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="IsHitTestVisible" Value="False">
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        </Trigger>
    </Style.Triggers>
</Style>

I've resolved this same problem by setting the Validation.ErrorTemplate property to null when the TextBox is hidden

<Style TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="IsHitTestVisible" Value="False">
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        </Trigger>
    </Style.Triggers>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文