Quantcast
Viewing all articles
Browse latest Browse all 18858

Why the use of ReadOnlyCollection in this Convertion method

Hi there:

I'm studying Data Validation in Bindings, and I ran accross this Validation method:

public class ErrorsToMessageConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        var sb = new StringBuilder();
        var errors = value as ReadOnlyCollection<ValidationError>;
        if (errors != null) {
            foreach (var e in errors.Where(e => e.ErrorContent != null)) {
                sb.AppendLine(e.ErrorContent.ToString());
            }
        }

        return sb.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        throw new NotImplementedException();
    }
}


This is the xaml that uses it:

<TextBlock Margin="2" Foreground="Red" FontWeight="Bold" 
        Text="{Binding ElementName=AddressBox, 
                        Path=(Validation.Errors),
                        Converter={StaticResource ErrorsToMessageConverter}}" />


It's clear that we are obtaining a collection of errors (Validation.Errors), but why do we use
ReadOnlyCollection<ValidationError> instead of just Collection or Array?, I tried changing it, and the app ceased to work!

Thanks in advance,

Rafael


Believe you can do it, and you will!!


Viewing all articles
Browse latest Browse all 18858

Trending Articles