wpf DataGrid.RowValidationErrorTemplate - 如何实际更改行外观而不是标题行?

时间:2021-11-16 09:13:23

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.

看来这只会影响我的标题行。有没有办法可以处理这个RowValidationErrorTemplate来修改行外观?我想把整行的背景变成红色或类似的东西。

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

有任何想法吗?如果我需要为此特定问题提供更多代码,请告诉我。提前致谢!

1 个解决方案

#1


5  

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

您可以更新DataGridRow类型的样式,并尝试根据行的Validation标志设置错误背景。

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.

如果这有帮助,请告诉我。

#1


5  

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

您可以更新DataGridRow类型的样式,并尝试根据行的Validation标志设置错误背景。

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.

如果这有帮助,请告诉我。