【发布时间】:2012-05-04 21:19:08
【问题描述】:
我正在尝试使用 IDataErrorInfo 接口在我的 WPF 应用程序中实现验证,但遇到了一个不太理想的情况。
我有这个模板,当控件验证失败时使用它
<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>
一切都很好,直到我尝试在控件上方显示验证失败的内容,例如在其上方显示停靠项:
我怎样才能避免这种情况并让我的错误模板显示在停靠项下方?
编辑
我发现我可以用AdornerDecorator 包装我的TextBox 来解决这个问题,但我真的不想为我的应用程序中的每个TextBox 控件执行此操作。有没有办法用Style 或其他方式设置它?
编辑 2
我可能会更改默认的TextBox ControlTemplate 以包含AdornerDecorator,但我不太热衷于更改任何 WPF 的默认控件模板。欢迎任何其他建议。
【问题讨论】:
标签: .net wpf validation xaml idataerrorinfo