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

Flo*_*anu 3 validation wpf templates row

我目前正在为我的数据网格使用行验证.我试图在行无效时更改行的外观.到目前为止我的代码在视觉上报告错误:

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

WPF*_*-it 6

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

像这样......

    <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>
Run Code Online (Sandbox Code Playgroud)

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