【问题标题】:How can I set the color of a selected row in DataGrid如何在 DataGrid 中设置选定行的颜色
【发布时间】:2010-11-16 10:25:36
【问题描述】:

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>

但还是什么都没有……

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    知道了。在 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>
    

    【讨论】:

    • 这太好了,我刚遇到这个,我很沮丧:-)
    • 你把它放在哪个部分?
    • 由于BorderBrush 保持蓝色,如果它惹恼了某人,只需添加另一个Setter 元素,它将BorderBrush 依赖属性设置为与Background 本身相同的颜色(值) .
    【解决方案2】:

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

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

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

    【讨论】:

    • 完美——正是我所需要的。允许我继续引用 DG 的现有样式并更改选定行的背景画笔。
    • 令人震惊。知道如何对前景色做同样的事情吗?
    • Arsen,只需像设置文本颜色一样覆盖 SystemColors.HighlightTextBrushKey 的画笔即可。
    • 终于找到了没有Focus的那个:SystemColors.ControlBrushKey.
    • @B.K.可能是SystemColors.InactiveSelectionHighlightBrushKey
    【解决方案3】:

    默认的 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>
    

    【讨论】:

    • @Mark H,与其在游戏后期对这个答案进行重大更改,不如添加自己的答案。
    【解决方案4】:

    作为@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 也适用于其他集合项,例如ListBoxItems(例如,如果您将TargetType="{x:Type DataGridRow}" 替换为TargetType="{x:Type ListBoxItem}")。

    【讨论】:

    • 您的解决方案是最好的。
    • 很好,谢里登。我还希望我的行在选定行上的前景色。但它需要默认的 HighlightTextBrushKey(即黑色)。怎么办?
    • @deathrace.dj,如果我理解正确,那么您只需将我的示例中第三个SolidColorbrushColor 属性更改为您喜欢的任何颜色。使用此声明实际上是设置SystemColors.HighlightTextBrushKey 使用的SolidColorbrush 的颜色。请注意,您没有在其他地方设置另一个Style 中的Foreground 颜色,因为这会覆盖上面Resources 中的设置。
    • 谢谢,这正是我想要的!
    • 您可能还想为SystemColors.InactiveSelectionHighlightBrushKey设置一个透明画笔,否则网格失去焦点时该行会突出显示
    【解决方案5】:

    我遇到了这个问题,几乎把头发扯断了,我无法在网上找到合适的答案。我试图控制 WPF DataGrid 中所选行的背景颜色。它只是不会这样做。就我而言,原因是我的数据网格中还有一个 CellStyle,并且 CellStyle 覆盖了我设置的 RowStyle。有趣的是,因为 CellStyle 甚至没有设置背景颜色,而是由 RowBackground 和 AlternateRowBackground 属性设置的。然而,当我这样做时,尝试设置所选行的背景颜色根本不起作用:

            <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>
    

    只是发布这个以防有人遇到同样的问题。

    【讨论】:

    • 这种情况每次都会发生,单元格设置一个前景并且每个主体都以该行为目标。
    • 我也遇到了这个问题,我什至没有设置 CellStyle。当我尝试在行样式上设置它时,它只影响非单元格元素。
    【解决方案6】:

    我尝试过 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>
    

    【讨论】:

      【解决方案7】:

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

      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>
      

      【讨论】:

        【解决方案8】:

        我花了一天的大部分时间来解决这个问题。原来我设置的 DataGrid 上的 RowBackground 属性覆盖了所有更改它的尝试。一旦我删除它,一切正常。 (顺便说一下,DataGridTextColumn 中的 Foreground 设置也是如此)。

        【讨论】:

          猜你喜欢
          • 2013-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-02
          • 1970-01-01
          • 2011-05-05
          • 2015-12-26
          • 2012-12-22
          相关资源
          最近更新 更多