错误模板应显示在其他控件上方,应将其隐藏 [英] Error template is displayed above other controls, when it should be hidden
问题描述
我正在尝试使用 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:
< img src = https://i.imgur.com/iZWwd.png alt =隐藏部分控件时显示>
如何避免并让我的错误模板按需显示在停靠项下方?
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.
这篇关于错误模板应显示在其他控件上方,应将其隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!