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

Jan*_*ter 121 c# wpf xaml datagrid

这看起来很简单,但我只是看不出怎么做.

DataGrid中所选行的默认背景颜色太深,我无法读取它.反正它有没有?

试过这个(从Neverminds链接修改)

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

但仍然没有......

Seb*_*ade 149

上面的解决方案在我的情

这是对我有用的解决方案.这很简单,只需添加到您的DataGrid.您可以将其从a更改SolidColorBrush为任何其他画笔,例如线性渐变.

<DataGrid.Resources>
  <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                   Color="#FF0000"/>
</DataGrid.Resources>
Run Code Online (Sandbox Code Playgroud)

  • Arsen,只需覆盖SystemColors.HighlightTextBrushKey的画笔,就像设置文本颜色一样. (7认同)
  • wounderfull.关于如何对前景色做同样的想法? (5认同)
  • 最后找到了一个如果它没有Focus:`SystemColors.ControlBrushKey`. (5认同)
  • @BK 可能是“SystemColors.InactiveSelectionHighlightBrushKey”。 (3认同)

Jan*_*ter 95

得到它了.在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>
Run Code Online (Sandbox Code Playgroud)

  • 你把它放在哪个部分? (6认同)
  • 由于`BorderBrush`仍然是蓝色,如果它使某人烦恼,只需添加另一个`Setter`元素,它将`BorderBrush`依赖属性设置为与`Background`本身相同的颜色(值). (4认同)

She*_*dan 72

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

您当然可以输入您喜欢的颜色.这Style也将为其他藏品,如工作ListBoxItem秒(如果更换TargetType="{x:Type DataGridRow}"TargetType="{x:Type ListBoxItem}"的实例).

  • 你的解决方案是最好的. (5认同)
  • 您可能还想为“SystemColors.InactiveSelectionHighlightBrushKey”设置透明画笔,否则当网格失去焦点时该行将突出显示 (4认同)

J S*_*J S 18

我遇到了这个问题,我差点把头发撕掉了,我无法在网上找到合适的答案.我试图控制WPF DataGrid中所选行的背景颜色.它只是不会这样做.在我的情况下,原因是我的数据网格中也有一个CellStyle,而CellStyle覆盖了我正在设置的RowStyle.有趣的是,因为CellStyle甚至没有设置背景颜色,而是由RowBackground和AlternateRowBackground属性设置bing.然而,当我这样做时,试图设置所选行的背景颜色根本不起作用:

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

当我将所选行的所需样式移出行样式并进入单元格样式时,它确实有效,如下所示:

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

只是发布这个以防有人有同样的问题.


gra*_*tnz 12

默认的IsSelected触发器会更改3个属性,Background,Foreground和BorderBrush.如果要更改边框和背景,只需在样式触发器中包含此内容即可.

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


小智 9

我遇到行选择事件的一些原因不起作用

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

由于我使用的是带有标签的模板列,因此我使用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>
Run Code Online (Sandbox Code Playgroud)


小智 5

我已经尝试过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>
Run Code Online (Sandbox Code Playgroud)