DataAnnotations OrderBy长度错误

时间:2017-01-18 20:12:38

标签: c# wpf

此方法返回属性错误(dataannotations)。我想要做的是返回按长度(ValidationMessage.Length)排序的错误。列表已排序,但问题出在视图中,它显示它们无序。你能帮我么?谢谢

   public void Validate(object currentInstance, string propertyName)
    {
        if (_validationErrors.ContainsKey(propertyName))
        {
            _validationErrors.Remove(propertyName);
        }

        var propertyInfo = currentInstance.GetType().GetProperty(propertyName);
        var propertyValue = propertyInfo.GetValue(currentInstance, null);
        var validationAttributes = propertyInfo.GetCustomAttributes(true).OfType<ValidationAttribute>();
        var validationErrors =
            validationAttributes
                .Select(
                    x => new CustomErrorType
                    {
                        ValidationMessage = x.FormatErrorMessage(string.Empty),
                        Severity = x.IsValid(propertyValue) ? Severity.SUCCESS : Severity.ERROR
                    }
                ).ToList().OrderBy(x => x.ValidationMessage.Length);;

        if (validationErrors.Any(x => x.Severity == Severity.ERROR))
        {
            _validationErrors.Add(propertyName, validationErrors);
        }
    }

的.xaml

    <Validation.ErrorTemplate>
        <ControlTemplate>
            <StackPanel>
                <AdornedElementPlaceholder x:Name="textBox" />
                <ItemsControl ItemsSource="{Binding}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding ErrorContent.ValidationMessage}">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Foreground"
                                                Value="Red" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding ErrorContent.Severity}"
                                                         Value="{x:Static customEnums:Severity.WARNING}">
                                                <Setter Property="Foreground"
                                                        Value="Orange" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding ErrorContent.Severity}"
                                                         Value="{x:Static customEnums:Severity.SUCCESS}">
                                                <Setter Property="Foreground"
                                                        Value="DarkGreen" />
                                                <Setter Property="FontWeight"
                                                        Value="Bold" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </ControlTemplate>
    </Validation.ErrorTemplate>

1 个答案:

答案 0 :(得分:1)

您可以绑定到对验证消息进行排序的CollectionViewSource:

<Validation.ErrorTemplate>
    <ControlTemplate>
        <StackPanel xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase">
            <StackPanel.Resources>
                <CollectionViewSource x:Key="cvs" Source="{Binding}">
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="ErrorContent"  />
                    </CollectionViewSource.SortDescriptions>
                </CollectionViewSource>
            </StackPanel.Resources>
            <AdornedElementPlaceholder x:Name="textBox" />
            <ItemsControl ItemsSource="{Binding Source={StaticResource cvs}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding ErrorContent}">
                            <TextBlock.Style>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Foreground" Value="Red" />
                                    <Style.Triggers>
                                                    <DataTrigger Binding="{Binding ErrorContent.Severity}"
                                                         Value="{x:Static customEnums:Severity.WARNING}">
                                                        <Setter Property="Foreground"
                                                        Value="Orange" />
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding ErrorContent.Severity}"
                                                         Value="{x:Static customEnums:Severity.SUCCESS}">
                                                        <Setter Property="Foreground"
                                                        Value="DarkGreen" />
                                                        <Setter Property="FontWeight"
                                                        Value="Bold" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
    </ControlTemplate>
</Validation.ErrorTemplate>