更改 UserControl 中 DataGrid 行的颜色

Change color of DataGrid row in UserControl

我在一个使用 UserControl 的项目中工作 我需要更改行颜色,同时我在网上冲浪寻找答案 我总是看到他们使用循环并使用 DataGrid.Rows 但它在 [=14 中不可用=] 那么如何更改为 System.Windows.Forms.DataGrid 或更改行的背景颜色 当我添加一个数据网格(拖放)时,它总是需要 System.Windows.Controls,当我使用 DataGrid.row 或 DataGrid.Rows 时,我得到这个错误

'System.Windows.Controls.DataGrid' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'System.Windows.Controls.DataGrid' could be found (are you missing a using directive or an assembly reference?)

你可以定义一个样式RowStyle

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Setter Property="Background" Value="Black" />
        <Setter Property="Foreground" Value="White" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Converter={StaticResource converter}}" Value="true"> <!-- You can create a converter to define your condition -->
                <Setter Property="Background" Value="Red" />
                <Setter Property="Foreground" Value="White" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>