Florin Bombeanu
Florin Bombeanu

Reputation: 840

wpf DataGrid.RowValidationErrorTemplate - how to actually change row appearance instead of header row?

I'm currently using row validation for my datagrid. I'm trying to change the appearance of a row when it is not valid. My code so far in terms of visually reporting the error:

<DataGrid.RowValidationErrorTemplate>
            <ControlTemplate>
                <Grid Margin="0,-2,0,-2" Background="Red" HorizontalAlignment="Stretch"
        ToolTip="{Binding RelativeSource={RelativeSource
        FindAncestor, AncestorType={x:Type DataGridRow}},
        Path=(Validation.Errors)[0].ErrorContent}">
                    <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" 
          FontWeight="Bold" Foreground="White" 
          HorizontalAlignment="Center"  />
                </Grid>
            </ControlTemplate>
        </DataGrid.RowValidationErrorTemplate>

It seems that this will only affect my header row. Is there a way I can handle this RowValidationErrorTemplate to modify the row appearance? I would like to make the entire row's background red or anything like that.

Any ideas? Please let me know if I need to provide more code for this particular problem. Thanks in advance!

Upvotes: 3

Views: 5236

Answers (1)

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

you can update the style of DataGridRow type and try to set error background based on Validation flag against the row.

Something like this...

    <DataGrid.Resources>
      <Style TargetType="{x:Type DataGridRow}"
             BasedOn="{StaticResource (x:Type DataGridRow)}"> <!--BasedOn is optional-->
            <Style.Triggers>
              <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="Background" Value="Red"/>        
              </Trigger>
            </Style.Triggers>
      </Style>
    </DataGrid.Resources>

Let me know if this helps.

Upvotes: 8

Related Questions