我试图通过控件上的工具提示来显示控件的多个验证错误,但没有找到实现此目的的方法。
我可以通过具有如下样式的工具提示轻松显示控件的单个验证错误:
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
</Trigger>
</Style.Triggers>
</Style>
但是,这种方法只能让我显示第一个验证错误。我尝试在工具提示中获取验证错误列表,但该列表从未显示。以下是我尝试过的方法:
将 ItemsControl 放置在工具提示中,使用 Validation.Errors 作为 ItemsSource,使用 CurrentItem.ErrorContent 作为要显示的文本:
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError"
Value="True">
<Setter Property="ToolTip">
<Setter.Value>
<ItemsControl>
<ItemsControl.ItemsSource>
<Binding Path="(Validation.Errors)" RelativeSource="{x:Static RelativeSource.Self}" />
</ItemsControl.ItemsSource>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding CurrentItem.ErrorContent}" Foreground="Red"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
我还尝试使用 Validation.Errors.CurrentItem 作为 ItemsSource,并使用 ErrorContent 作为要显示的文本:
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError"
Value="True">
<Setter Property="ToolTip">
<Setter.Value>
<ItemsControl>
<ItemsControl.ItemsSource>
<Binding Path="(Validation.Errors).CurrentItem" RelativeSource="{x:Static RelativeSource.Self}" />
</ItemsControl.ItemsSource>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent}" Foreground="Red"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
这些方法中的任何一种都会导致显示空的工具提示:
我可以使用 Validation.ErrorTemplate 将控件的验证错误列表显示为控件外部的列表:
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<AdornedElementPlaceholder/>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent}" Foreground="Red"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This https://stackoverflow.com/questions/4161523/wpf-validation-errors-setting-tooltip-with-error-message/14550973#14550973答案展示了如何使用 ErrorTemplate 将错误放置在单独的元素工具提示中。似乎在 ErrorTemplate 中,您可以访问完整的错误列表,而我在上面尝试的工具提示样式中似乎没有这些列表。答案中的第一个解决方案使控件不可点击,这是我不想要的。我在这里尝试了答案中的第二个解决方案:
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Grid>
<Ellipse Fill="Red" Opacity="0.8" Width="10" Height="10" HorizontalAlignment="Right" VerticalAlignment="Top">
<Ellipse.ToolTip>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent}" Foreground="Red"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Ellipse.ToolTip>
</Ellipse>
<AdornedElementPlaceholder />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这确实给了我一个工具提示,其中包含控件的所有错误:
但是,您必须将鼠标悬停在红色圆圈上才能显示工具提示。我的目标是当您将鼠标悬停在控件上时出现工具提示,我认为这对用户来说会更直观。我似乎不知道如何将多个错误放入实际的控制工具提示中。这可能吗?