我遇到了一个非常愚蠢的问题 - 需要在 WPF DataGrid 中设置所选行的样式。
我想显示一个带有蓝色边框的矩形,而不是仅仅用某种颜色填充整行。
有什么想法可以实现吗?它只是必须有某种方法让它变得非常简单。
最佳答案
在DataGrid
上使用CellStyle
和RowStyle
。 DataGridCell
和 DataGridRow
都有 IsSelected
属性,可在 Trigger
中使用该属性来确定它们是否被选中。
像下面这样的东西应该可以解决问题:
<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>
尝试一下,直到你做对为止。
关于WPF DataGrid 选定的行样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4539909/