我正在尝试通过控件上的工具提示显示控件的多个验证错误,但没有找到执行此操作的方法。
我可以通过具有以下样式的工具提示轻松显示控件的单个验证错误:
<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>
Run Code Online (Sandbox Code Playgroud)

但是,这种方法只让我显示第一个验证错误。我试图在工具提示中获取验证错误列表,但该列表从未显示。以下是我尝试过的方法:
在工具提示中放置一个 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>
Run Code Online (Sandbox Code Playgroud)
我还尝试使用 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>
Run Code Online (Sandbox Code Playgroud)
这些方法中的任何一种都会导致显示空工具提示:

我可以使用 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>
Run Code Online (Sandbox Code Playgroud)

此答案显示了如何使用 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>
Run Code Online (Sandbox Code Playgroud)
这确实给了我一个包含控件所有错误的工具提示:

但是,您必须将鼠标悬停在红色圆圈上才能显示工具提示。我的目标是当您将鼠标悬停在控件上时显示工具提示,我认为这对用户来说会更直观。我似乎无法找出如何将多个错误放入实际控制工具提示中。这可能吗?
我知道这个问题是不久前发布的,但我遇到了同样的问题,想分享我的解决方案(希望它可以帮助其他人;))。
经过一番谷歌搜索后,我发现了一篇描述这个问题的帖子。他们建议用多重绑定来解决这个问题,我就是这么做的。我只使用了一种样式来执行此操作,
这里是该样式的代码:
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="ToolTip">
<Setter.Value>
<MultiBinding Converter="{StaticResource conv}">
<Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)" />
<Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors).Count" />
</MultiBinding>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
问题是 (Validation.Errors) 为您提供一个 ReadOnlyObservableCollection<ValidationError> 并且该集合将随着时间的推移而被填充。但是当您绑定到 (Validation.Errors) 时,它只会注意到第一项。这就是第二个绑定 (Validation.Errors).Count 的原因,以便每次更改计数时都会调用您的转换器。
现在我们终于需要转换器:
public class MultiLineConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (!(values[0] is IEnumerable<ValidationError>))
return null;
var val = values[0] as IEnumerable<ValidationError>;
string retVal = "";
foreach (var itm in val)
{
if (retVal.Length > 0)
retVal += "\n";
retVal += itm.ErrorContent;
}
return retVal;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
它只使用值[0],因为那是集合。然后它会为所有 ErrorContents 创建一个字符串。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
2304 次 |
| 最近记录: |