C# 如何在 DataGrid 中设置选定行的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1223280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set the color of a selected row in DataGrid
提问by Jan Bannister
The default background color of a selected row in DataGrid is so dark that I can't read it. Is there anyway of overriding it?
DataGrid 中选定行的默认背景颜色太深,我无法阅读。反正有没有覆盖它?
Tried this
试过这个
<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>
But still nothing...
但是还是没有...
采纳答案by Jan Bannister
Got it. Add the following within the DataGrid.Resources section:
知道了。在 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>
回答by Seb Kade
The above solution left blue border around each cell in my case.
在我的例子中,上述解决方案在每个单元格周围留下了蓝色边框。
This is the solution that worked for me. It is very simple, just add this to your DataGrid
. You can change it from a SolidColorBrush
to any other brush such as linear gradient.
这是对我有用的解决方案。这很简单,只需将其添加到您的DataGrid
. 您可以将其从 a 更改SolidColorBrush
为任何其他画笔,例如线性渐变。
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="#FF0000"/>
</DataGrid.Resources>
回答by grantnz
The default IsSelected trigger changes 3 properties, Background, Foreground & BorderBrush. If you want to change the border as well as the background, just include this in your style trigger.
默认的 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>
回答by Sheridan
As an extention to @Seb Kade's answer, you can fully control the colours of the selected and unselected rows using the following 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>
You can of course enter whichever colours you prefer. This Style
will also work for other collection items such as ListBoxItem
s (if you replace TargetType="{x:Type DataGridRow}"
with TargetType="{x:Type ListBoxItem}"
for instance).
您当然可以输入您喜欢的任何颜色。这Style
也将为其他藏品的工作,如ListBoxItem
S(如果更换TargetType="{x:Type DataGridRow}"
用TargetType="{x:Type ListBoxItem}"
的实例)。
回答by J S
I had this problem and I nearly tore my hair out, and I wasn't able to find the appropriate answer on the net. I was trying to control the background color of the selected row in a WPF DataGrid. It just wouldn't do it. In my case, the reason was that I also had a CellStyle in my datagrid, and the CellStyle overrode the RowStyle I was setting. Interestingly so, because the CellStyle wasn't even setting the background color, which was instead bing set by the RowBackground and AlternateRowBackground properties. Nevertheless, trying to set the background colour of the selected row did not work at all when I did this:
我遇到了这个问题,我差点把头发扯掉,我无法在网上找到合适的答案。我试图控制 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>
and it did work when I moved the desired style for the selected row out of the row style and into the cell style, like so:
当我将所选行的所需样式从行样式移到单元格样式时,它确实起作用了,如下所示:
<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>
Just posting this in case someone has the same problem.
只是发布这个以防有人遇到同样的问题。
回答by Kenneth Zhang
I've tried ControlBrushKey but it didn't work for unselected rows. The background for the unselected row was still white. But I've managed to find out that I have to override the rowstyle.
我试过 ControlBrushKey 但它对未选择的行不起作用。未选择行的背景仍然是白色的。但是我设法发现我必须覆盖 rowstyle。
<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>
回答by Anupam Dutta
Some of the reason which I experienced the row selected event not working
我遇到行选择事件不起作用的一些原因
- Style is set up for DataGridCell
- Using Templated columns
- Trigger is set up at the DataGridRow
- 为 DataGridCell 设置了样式
- 使用模板列
- 触发器设置在 DataGridRow
This is what helped me. Setting the Style for DataGridCell
这就是帮助我的原因。设置 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>
And since I was using a template column with a label inside, I bound the Foreground property to the container Foreground using RelativeSource binding:
由于我使用了一个内部带有标签的模板列,因此我使用 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>
回答by user947737
I spent the better part of a day fiddling with this problem. Turned out the RowBackground Property on the DataGrid - which I had set - was overriding all attempts to change it in . As soon as I deleted it, everything worked. (Same goes for Foreground set in DataGridTextColumn, by the way).
我花了一天的大部分时间摆弄这个问题。结果发现 DataGrid 上的 RowBackground 属性(我已设置)覆盖了在 . 当我删除它时,一切正常。(顺便说一下,DataGridTextColumn 中的 Foreground 设置也是如此)。