我想为带有边框和多边形的文本框创建验证模板。 我有文本框的下一个错误模板:
<ResourceDictionary>
<ControlTemplate x:Key="ErrorTemplate" TargetType="{x:Type Control}">
<Grid>
<Border
Background="#11FF0000"
BorderBrush="#FFFF0000"
BorderThickness="1"
IsHitTestVisible="False"/>
<Polygon
Fill="#FFFF0000"
Points="0,0 10,0 10,10"
HorizontalAlignment="Right"
ToolTip="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
<AdornedElementPlaceholder x:Name="adorner"/>
</Grid>
</ControlTemplate>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorTemplate}"/>
</Style>
</ResourceDictionary>
如何在工具提示中显示所有验证错误? 我知道如何在列表框(http://prntscr.com/ltk6yv)中显示所有错误,但不知道如何将其与面组合
<ControlTemplate x:Key="ErrorTemplate" TargetType="{x:Type Control}">
<StackPanel>
<ListBox ItemsSource="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ErrorContent}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<AdornedElementPlaceholder x:Name="adorner"/>
</StackPanel>
答案 0 :(得分:1)
将ToolTip
属性设置为ItemsControl
:
<ControlTemplate x:Key="ErrorTemplate" TargetType="{x:Type Control}">
<Grid>
<Border Background="#11FF0000" BorderBrush="#FFFF0000" BorderThickness="1" IsHitTestVisible="False"/>
<Polygon
Fill="#FFFF0000"
Points="0,0 10,0 10,10"
HorizontalAlignment="Right">
<Polygon.ToolTip>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Polygon.ToolTip>
</Polygon>
<AdornedElementPlaceholder x:Name="adorner"/>
</Grid>
</ControlTemplate>