WPF DataGrid 选定的行样式 [英] WPF DataGrid selected row style
问题描述
我遇到了一个非常愚蠢的问题 - 需要在 WPF DataGrid 中设置所选行的样式.
I'm stuck with one very stupid problem - need to style selected row in WPF DataGrid.
我想显示一个带有蓝色边框的矩形,而不是用某种颜色填充整行.
I want to show a rectangle with blue border instead of just filling entire row with some color.
任何想法如何实现?一定是某种方式让它变得非常简单.
Any ideas how to implement this? It just must be some way to make it pretty easy.
推荐答案
在 DataGrid
上使用 CellStyle
和 RowStyle
.DataGridCell
和 DataGridRow
都具有 IsSelected
属性,可在 Trigger
中使用以查看它们是否被选中.
Use CellStyle
and RowStyle
on DataGrid
. DataGridCell
and DataGridRow
both have IsSelected
property that can be used in a Trigger
to find out if they are selected.
类似以下内容应该可以解决问题:
Something like following should do the trick:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="White" />
<Setter Property="Foreground"
Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="BorderBrush"
Value="Blue" />
<Setter Property="BorderThickness"
Value="2" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
随便玩玩,直到你做对了.
Just play around until you get it right.
这篇关于WPF DataGrid 选定的行样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!