Search code examples
c#wpfvalidationdatagridformatting

How do I get WPF DataGrid bespoke error styling to work for value converter errors?


Context

WPF MVVM application where I need to allow the display and editing of datetime dates from a viewmodel. Some dates are "free standing" and some are in a DataGrid. Error handling (range checks etc) is through INotifyDataErrorInfo and a validation service. I style dates in error like this so as to highlight them and provide a tooltip with an error message:

<Style x:Key="ErrorStyle" TargetType="{x:Type Control}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <ToolTip DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}">
                        <ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}" DisplayMemberPath="ErrorContent" />
                    </ToolTip>
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="Salmon"/>
            <Setter Property="Height" Value="20"/>
        </Trigger>                
    </Style.Triggers>
</Style>

Issue

Applying this to "free standing" dates everything works fine e.g.

<TextBox x:Name="dueDate"
         Style="{StaticResource ErrorStyle}"
         Text="{Binding Path=DueDate,
                        StringFormat=d, ConverterCulture={x:Static gl:CultureInfo.CurrentCulture},
                        ValidatesOnNotifyDataErrors=True}">
</TextBox>

This works both when a value converter error occurs (e.g. invalid date format) or an error picked up by the validation service.

However for dates in the DataGrid things are different:

<DataGridTemplateColumn Header="Delivery date" SortMemberPath="DeliveryDate">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=DeliveryDate, StringFormat=d, ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}"
                     Style="{StaticResource ErrorStyle}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding DeliveryDate, Mode=TwoWay, UpdateSourceTrigger=LostFocus,
                            StringFormat=d,
                            ConverterCulture={x:Static gl:CultureInfo.CurrentCulture},
                            ValidatesOnNotifyDataErrors=True}"/>                                               
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

Here my style is applied for errors spotted by the validation service. But, for value converter errors I get a red surround, no tooltip and all other fields are disabled. I gather the later is the default behavior such errors.

Question

How can I get my DataGrid date error formating to behave the same way as for my free standing dates?


Solution

  • Finally solved this. I needed to add my ErrorStyle to the CellEditingTemplate as well as to the CellTemplate.