我使用的是.NET Framework 4.5,我遇到了以下问题。
如果我像这样添加验证错误模板,我的装饰工具将工作并在我的TextBox旁边显示工具提示和红色圆圈就好了:
CAST(YourDateTimeColumn AS DATE)
因此,上面的验证模板已添加到我的TextBox txtAge的// THIS IS WORKING FINE BUT ONLY FOR txtAge TextBox
<TextBox x:Name="txtAge"
Validation.Error="Validation_Error"
Text="{Binding UpdateSourceTrigger=LostFocus, Path=Age, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
HorizontalAlignment="Left" MaxLength="3" Width="50">
<Validation.ErrorTemplate>
<ControlTemplate>
<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>
</Validation.ErrorTemplate>
</TextBox>
标记内,因此仅适用于该TextBox。
但是,我希望有一个适用于所有TextBox的样式,所以我在<TextBox>
标签内添加了Adorner。但这既不会显示工具提示也不会显示红色圆圈:
<Window.Resources>
为什么第一个工作,第二个不工作?我是WPF的新手。
答案 0 :(得分:1)
基于@Snowball两个回答,我想通了。
将代码移动到Windows.Resources部分并添加x:Key如下所示:
<ControlTemplate x:Key="ValidationTemplate">
<DockPanel LastChildFill="true">
<Border Background="Red" DockPanel.Dock="right"
Margin="5,0,0,0" Width="10" Height="10" 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>
然后对于每个TextBox,添加以下行以引用ControlTemplate
Validation.ErrorTemplate="{StaticResource ValidationTemplate}"