1

In my WPF application (.Net 4.5) I want to provide extended visual feedback about validation results to the UI. The data layer's validation engine returns warnings and errors via INotifyDataErrorInfo interface.

I have the following XAML to display red or orange border depending on the error type and the list of error messages. Here errorToColor is a resource key of the value converter which returns red brush if there is at least one error in the Validation.Errors collection and orange brush if there are only warnings.

        <TextBox Name="MappingName" Text="{Binding Path=Mapping.Name, NotifyOnValidationError=True}" >
            <Validation.ErrorTemplate>
                <ControlTemplate>
                    <DockPanel>
                        <Border BorderBrush="{Binding Converter={StaticResource errorsToColor}}" BorderThickness="1">
                            <AdornedElementPlaceholder />
                        </Border>
                        <ListView DisplayMemberPath="ErrorContent" ItemsSource="{Binding}" />
                    </DockPanel>
                </ControlTemplate>
            </Validation.ErrorTemplate>
        </TextBox>

Now let's see what happens when I type some 'not valid' text in the TextBox.

  • Typed 'Text1' and changed focus.
    Debugger stepped into the converter and both validators resulting in two items in the ListView (1 error and 1 warning) and a red border. [OK]
  • Typed 'Text' to correct the error, changed focus.
    The value converter wasn't even hit! Of course, the same red border. But the ListView has changed and shows only one warning.

Can somebody explain what's going on? Why the ListView receives collection change notification and the Border not? Is it because ListView is an ItemsControl and the Validation.Errors is wrapped into the CollectionView?

yuvin
  • 363
  • 1
  • 12
  • It is probably related to http://stackoverflow.com/questions/2816163/when-will-the-valueconverters-convert-method-be-called-in-wpf Adding new item to Validation.Errors raises CollectionChanged event while PropertyChanged triggers value converters – yuvin Oct 04 '12 at 13:33

1 Answers1

1

For those who are interested. The errorsToColor converter wasn't fired because the Validation.Errors collection didn't raise PropertyChanged event (needed trigger binding coverter) when errors were added or removed.

In order to raise PropertyChanged event we need to bind to a property which is changed on each error is added, for example Count. I still need the Errors collection itself in the converter, so I used a multi-binding here.

            <Border BorderThickness="1">
                <Border.BorderBrush>
                    <MultiBinding Converter="{StaticResource errorsToColor}">
                        <Binding Path="." />
                        <Binding Path=".Count" />
                    </MultiBinding>
                </Border.BorderBrush>
                <AdornedElementPlaceholder Name="adornedElement" />
            </Border>

Now the errorsToColor converter (which is now implements IMultiValueConverter) is executed every time new error is added / removed.

yuvin
  • 363
  • 1
  • 12