0
Vote

Allow the user to define field display names when daeling with validation services in view models

description

The issue is that actually the ValidationError brings only the information that a given "property name" is not valid, and the list of error messages, obviously the end user prefer too see a reasonable display name instead of the property name, on solution is the one adopted by Aurelio to add this information directly to the ValidationError class, this really complicates stuff when dealing with localization, the display name should be dynamacally discovered using some sorrt of behavior.
Using the following as a validation summary:
    <ItemsControl Grid.IsSharedSizeScope="True"
              ItemsSource="{Binding Path=ValidationErrors}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="a" />
                        <ColumnDefinition Width="*" SharedSizeGroup="b" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Column="0"
                               Margin="0,0,5,0"
                               Foreground="Red"
                               Text="{Binding Key}" />
                    <TextBlock Grid.Column="1" Text="{Binding}" />
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
Instead of binding the text property of the item template to the key (Text="{Binding Key}") we should use a custom markup extension that given the following markup

Text="{markup:DisplayNameBinding Path=Key}"

We should attempt, in the following order:
  • Look on the property for a DisplayName attribute;
  • Look on the property for some custom attribute that allows to link resources
  • look if the ViewModel class implements the following interface:

    interface IProvidePropertyDisplayName
    {
    Boolean TryGetDisplayName( String propertyName, out String displayName );
    }

    (using the Try* pattern so to allow the cascading of the lookup if we return false)
  • Look for some default resource location using the namespace + class name + property name as the key.

comments