【发布时间】:2015-07-05 20:57:01
【问题描述】:
我遇到了如何在垂直 StackPanel 中为 TextBox 元素显示验证错误的问题。我正在尝试在 TextBox 下方显示错误消息。
我有这个错误模板:
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<AdornedElementPlaceholder />
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding (ValidationError.ErrorContent)}" Foreground="Red" Margin="5"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
如果我在 TextBox 下方有足够的空白,则错误会很好地显示,但在 StackPanel(例如)中,由于装饰层,它不会为错误消息添加额外的边距或填充。
预计会是这样,根据this source:
请注意,Validation.ErrorTemplate 将显示在装饰层上。装饰层中的元素呈现在其余可视元素之上,并且在布局系统测量和排列装饰元素层上的控件时不会考虑它们。
如何显示验证错误消息,以便它们不会显示在 StackPanel 中的其他元素之上?
【问题讨论】:
标签: wpf validation xaml errortemplate