如何创建工具提示以显示单个控件的多个验证错误?

时间:2015-06-03 17:58:00

标签: wpf xaml validation

我试图通过控件上的工具提示显示控件的多个验证错误,但我找不到这样做的方法。

我可以通过具有如下样式的工具提示轻松地为控件显示单个验证错误:

<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>

enter image description here

但是,这种方法只允许我显示第一个验证错误。我试图在工具提示中获取验证错误列表,但列表从不显示。以下是我尝试过的方法:

将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>

这些方法中的任何一种都会导致显示空的工具提示:

enter image description here

我可以使用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>

enter image description here

This回答显示了如何使用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>

这确实为我提供了一个工具提示,其中包含控件的所有错误:

enter image description here

但是,您必须将鼠标悬停在红色圆圈上才能显示工具提示。我的目标是当您将鼠标悬停在控件上时显示工具提示,我认为这对用户来说更直观。我似乎无法找到如何将多个错误放入实际控制工具提示中。这可能吗?

2 个答案:

答案 0 :(得分:2)

我知道这个问题是前一段时间发布的,但我遇到了同样的问题并希望分享我的解决方案(希望它可以帮助其他人);)。

经过一段时间的googeling后,我找到了post来描述这个问题。他们建议用MultiBinding解决这个问题,这就是我所做的。我只用了一种风格来做这件事 这里是样式的代码:

<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>

问题是(Validation.Errors)获取ReadOnlyObservableCollection&lt; ValidationError&gt;这个系列将随着时间的推移而填补。但是当你绑定到(Validation.Errors)时,它只会注意到第一个项目。这就是第二次绑定(Validation.Errors).Count的原因,这样每次更改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();
    }
}

它只使用值[0],因为那是集合。然后它将为所有ErrorContents创建一个字符串。

希望这有帮助。

答案 1 :(得分:1)

这已经过时了,但我坚持了一段时间,我发现了一种我更喜欢的方式。 它不会将您的验证错误转换为字符串,而是使用ItemsControl,这对我来说更有意义。

工具提示不是窗口可视树的一部分,但您仍可以为其创建样式。 Validation.ErrorsValidationError个对象的集合。

<Style x:Key="validationTooltipStyle" TargetType="{x:Type ToolTip}">
            <Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToolTip">
                        <ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}" Background="#FFDC000C" Margin="0 0 3 0">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding ErrorContent}" Foreground="#FFFFFF"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
</Style>

实例化工具提示资源

<ToolTip x:Key="validationTooltip" Style="{StaticResource validationTooltipStyle}"/>

最后将其设置为在目标有错误时触发

<Style TargetType="ComboBox">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">>
            <Setter Property="ToolTip" Value="{StaticResource validationTooltip}"/>
        </Trigger>
    </Style.Triggers>
</Style>