错误模板应显示在其他控件上方,应将其隐藏

编程入门 行业动态 更新时间:2024-10-04 01:21:17
本文介绍了错误模板应显示在其他控件上方,应将其隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 IDataErrorInfo 接口在WPF应用程序中实现验证,并且遇到了不太理想的情况。

I'm trying to implement validation in my WPF application using the IDataErrorInfo interface, and I've encountered a not-so-desirable situation.

我有此模板,当控件无法通过验证时使用

I have this template which is used when a control fails to validate

<ControlTemplate x:Key="errorTemplate"> <DockPanel LastChildFill="true"> <Border Background="Red" DockPanel.Dock="Right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"> <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" /> </Border> <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" > <Border BorderBrush="red" BorderThickness="1" /> </AdornedElementPlaceholder> </DockPanel> </ControlTemplate>

一切正常,直到我尝试在控件上方显示无法通过验证的内容,例如显示停靠栏上面的项目:

Everything is well until I try to display something above the control that failed validation, such as displaying a dock item above it:

如何避免并让我的错误模板按需显示在停靠项下方?

How can I avoid this and make my error template displayed below the dock item, as it should?

EDIT

我发现我可以用 AdornerDecorator 包装我的 TextBox 来解决此问题,但我确实不愿意不想对应用程序中的每个 TextBox 控件执行此操作。

I found that I could wrap my TextBox with an AdornerDecorator to fix this, but I really don't want to do this for each and every TextBox control in my application. Is there maybe a way to set it with a Style or some other way?

EDIT 2 Style 或其他方式可以设置它吗? / strong>

EDIT 2

我可能会更改默认的 TextBox ControlTemplate,使其包含 AdornerDecorator ,但是我不太想更改WPF的默认控件模板。欢迎其他任何建议。

I could probably change the default TextBox ControlTemplate to include an AdornerDecorator, but I'm not too keen on changing any of WPF's default control templates. Any other suggestions are welcome.

推荐答案

好的,我发现了一个相对简单的解决方案,不会强迫我更改任何控制模板。

OK, I found a relatively simple solution which doesn't force me to change any control templates.

而不是用 AdornerDecorator 装饰每个 TextBox 像这样

Instead of decorating each TextBox with an AdornerDecorator like this

<StackPanel> <AdornerDecorator> <TextBox Text={Binding ...} /> </AdornerDecorator> <AdornerDecorator> <TextBox Text={Binding ...} /> </AdornerDecorator> </StackPanel>

我可以让 AdornerDecorator 包裹我的整个包裹

I can have the AdornerDecorator wrap my entire view, which achieves the same result.

<AdornerDecorator> <StackPanel> <TextBox Text={Binding ...} /> <TextBox Text={Binding ...} /> </StackPanel> </AdornerDecorator>

这样,我最多可以为每个视图定义一次。

This way I can define it at most one time per view.

更多推荐

错误模板应显示在其他控件上方,应将其隐藏

本文发布于:2023-11-03 04:38:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将其   控件   模板   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!