我正在尝试更改 WPF DataGrid 控件中选定单元格的样式。
样式化 DataGridCell
和 DataGridRow
之间有什么区别?我尝试了下面的各种组合,它们都有效。但是,似乎我只需要设置 either DataGridCell
或 DataGridRow
的样式。
两者的目的是什么?这告诉我我误解了他们的目的。
DataGridCell
的样式:(在我自己的代码中,我更改了默认颜色)
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
DataGridRow
的样式:(在我自己的代码中,我更改了默认颜色)
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
最佳答案
DataGridRow 将应用于整个行。如果要设置整行的任何属性,可以使用 DataGridRow。
每个 DataGridRow 包含一个 DataGridCell 列表。您也可以为其定义样式。如果不这样做,它将采用 DataGridRow 的样式。
通常,我们指定后者来定义两个相邻单元格之间的区别,例如指定单元格之间的边距、边框等。
关于c# - 在 WPF DataGrid 控件中设置选定单元格的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12831173/