如何在 DataGrid 中设置选定行的颜色

DataGrid 中选定行的默认背景颜色太暗,我无法读取。有什么办法能覆盖它吗?

试试这个

<dg:DataGrid.RowStyle>
<Style TargetType="{x:Type dg:DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="Background" Value="Gainsboro" />
</Trigger>
</Style.Triggers>
</Style>
</dg:DataGrid.RowStyle>

但还是没有..。

165275 次浏览

明白了。在 DataGrid. Resources 部分添加以下内容:

  <DataGrid.Resources>
<Style TargetType="{x:Type dg:DataGridCell}">
<Style.Triggers>
<Trigger Property="dg:DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="#CCDAFF" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>

在我的示例中,上述解决方案在每个单元格周围都留下了蓝色边框。

这是对我有效的解决办法。这是非常简单的,只需添加到您的 DataGrid。您可以将其从 SolidColorBrush改为任何其他画笔,如线性渐变。

<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="#FF0000"/>
</DataGrid.Resources>

默认的 IsSelected 触发器更改3个属性: 背景、前景和边框笔刷。如果您想更改边框和背景,只需在样式触发器中包含此内容即可。

<Style TargetType="{x:Type dg:DataGridCell}">
<Style.Triggers>
<Trigger Property="dg:DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="#CCDAFF" />
<Setter Property="BorderBrush" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>

作为@Seb Kade 答案的一个扩展,你可以使用以下 Style来完全控制选中和未选中行的颜色:

<Style TargetType="{x:Type DataGridRow}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>

你当然可以输入任何你喜欢的颜色。这个 Style也适用于其他集合项,如 ListBoxItem(例如,如果用 TargetType="{x:Type ListBoxItem}"替换 TargetType="{x:Type DataGridRow}")。

我有这个问题,我几乎扯掉我的头发,我不能找到适当的答案在网上。我试图在 WPF DataGrid 中控制所选行的背景颜色。就是不行。在我的例子中,原因是我的数据网格中也有一个 CellStyle,并且 CellStyle 覆盖了我正在设置的 RowStyle。有趣的是,因为 CellStyle 甚至没有设置背景颜色,而是由 RowContext 和 AlternateRowbackitproperties 设置。然而,当我这样做时,试图设置所选行的背景颜色根本不起作用:

        <DataGrid ... >
<DataGrid.RowBackground>
...
</DataGrid.RowBackground>
<DataGrid.AlternatingRowBackground>
...
</DataGrid.AlternatingRowBackground>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Pink"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="{Binding MyProperty}" />
</Style>
</DataGrid.CellStyle>

当我将所选行的所需样式从行样式移到单元格样式时,它确实起作用了,如下所示:

    <DataGrid ... >
<DataGrid.RowBackground>
...
</DataGrid.RowBackground>
<DataGrid.AlternatingRowBackground>
...
</DataGrid.AlternatingRowBackground>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="{Binding MyProperty}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Pink"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>

发布这个,以防有人也有同样的问题。

我已经试过 ControlBrushKey,但它对未选择的行无效。未选定行的背景仍为白色。但是我发现我必须重写行样式。

<DataGrid x:Name="pbSelectionDataGrid" Height="201" Margin="10,0"
FontSize="20" SelectionMode="Single" FontWeight="Bold">
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFFDD47C"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FFA6E09C"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Violet"/>
</DataGrid.Resources>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="LightBlue" />
</Style>
</DataGrid.RowStyle>
</DataGrid>

我所经历的行选择事件不工作的一些原因

  1. 为 DataGridCell 设置样式
  2. 使用模板列
  3. 在 DataGridRow 上设置了触发器

这对我很有帮助。设置 DataGridCell 的样式

<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>

因为我使用了一个带有标签的模板列,所以我使用 RelativeSource 绑定将 Foreground 属性绑定到容器 Foreground:

<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding CategoryName,
Mode=TwoWay,
UpdateSourceTrigger=LostFocus}"
Foreground="{Binding Foreground,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorLevel=1,
AncestorType={x:Type DataGridCell}}}"
Width="150"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我花了一天的大部分时间来处理这个问题。原来 DataGrid 上的 RowBackingProperty (我已经设置好了)覆盖了所有更改它的尝试。我删掉之后,一切都正常了。 (顺便说一句,DataGridTextColumn 中的 Foreground 设置也是如此)。