DataGridCell Validation.ErrorTemplate ignored
Asked Answered
T

3

6

I'm trying to set the Validation.ErrorTemplate of the DataGridCells, here's the xaml code:

<Style x:Key="{x:Type DataGridCell}"  x:Uid="dataGridCellErrorTemplate" TargetType="{x:Type DataGridCell}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate >
                <Border BorderBrush="Green" BorderThickness="2" ToolTip="Heidenei"></Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <!-- following line only for demonstration that the setter is working ... -->
    <Setter Property="Background" Value="Aquamarine"></Setter>              
</Style>

while the background of the datagridcells is successfully colored green (independant from any validation result) the used Validation.ErrorTemplate is still the default one, i.e. the red border.

I know there have been similar issues here in stackoverflow, e.g. Styling DataGridCell Error Template but they do not really solve my problem.

Any help is appreciated

Frank

Trichinize answered 15/3, 2012 at 14:34 Comment(3)
Because the editing controls are created at run time, you cannot use the Validation.ErrorTemplate attached property like you would with simple controls. you may have to do this after rendering (DispatcherTimer)Concretize
Thanks for your feedback, but I'm not sure if I understand it. In my scenario the datagridcells are not edited by the user (therefore no editing controls), but by a background process. I still want to use IDataErrorInfo to highlight those fields that have problematic values.Trichinize
when they say "editing controls", (i think) they mean "the controls embedded in each DataGridCell". in other words, the Template may get applied to the cell, BUT it's overwritten when the inner control is created. that's why you might have to capture when a Row is created, and loop through each cell to set your own value..Concretize
C
7

I believe that I'm experiencing the same issue.

When using a DataGridTemplateColumn the content is presented with a ContentPresenter. This content presenter uses the default error template.

I can't find a direct way to remove this template for an individual DataGridTemplateColumn but you can remove it for all content presenters in the DataGrid by adding a style to the DataGrid's resources.

<DataGrid.Resources>
    <Style TargetType="ContentPresenter">
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
    </Style>
</DataGrid.Resources>
Cash answered 20/2, 2013 at 23:12 Comment(2)
Wish I had found your answer sooner... spent all morning trying to figure this one out. I'm using several DataGridTemplateColumn with their own custom validation templates and I needed to remove the default red border around the cell. This did the trick.Dislocate
Thanks a lot, it's not so easy to find the solution !Huonghupeh
C
1

I had luck removing the irritating red border by using the following TextBlock style.

<Style TargetType="TextBlock">
    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
</Style>
Chinaware answered 17/9, 2014 at 11:29 Comment(0)
C
0

From Microsoft, attempting to change the ErrorTemplate on a DataGrid just will not work. Their example is a trigger in a style where you set the background as red (or whatever) when the Validation.HasError property is true.

To customize cell validation feedback: Set the column's EditingElementStyle property to a style appropriate for the column's editing control. Because the editing controls are created at run time, you cannot use the Validation.ErrorTemplate attached property like you would with simple controls.

This approach worked for me, and it also removed the default error template (the red border around the cell). The red border is replaced with the BorderBrush in the style, and the little exclamation point can be removed by setting the RowValidationErrorTemplate property of DataGrid to {x:Null}. I have looked at the other related questions on SO and have not found this solution anywhere. Here is an example of my solution, with the style definition and a DataGridTextColumn which uses the style:

    <Style
        x:Key="DataGridTextBoxValidationStyle"
        TargetType="{x:Type TextBox}">
        <Setter Property="Padding" Value="-2" />
        <Setter Property="MaxWidth" Value="250"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Path=ActualWidth}"/>
        <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Path=ActualHeight}"/>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="Background" Value="Firebrick" />
                <Setter Property="Foreground" Value="White" />
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                <Setter Property="BorderBrush" Value="Firebrick"/>
            </Trigger>
        </Style.Triggers>
    </Style>

<DataGrid RowValidationErrorTemplate="{x:Null}">
    <DataGrid.Columns>
        <DataGridTextColumn
            EditingElementStyle="{StaticResource DataGridTextBoxValidationStyle}">
             <!-- other stuff here -->
        </DataGridTextColumn>
   </DataGrid.Columns>
</DataGrid>
Cule answered 24/10, 2022 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.