c# - 在 WPF DataGrid 控件中设置选定单元格的样式

标签 c# wpf xaml styles

我正在尝试更改 WPF DataGrid 控件中选定单元格的样式。

样式化 DataGridCellDataGridRow 之间有什么区别?我尝试了下面的各种组合,它们都有效。但是,似乎我只需要设置 either DataGridCellDataGridRow 的样式。

两者的目的是什么?这告诉我我误解了他们的目的。

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/

相关文章:

c# - Outlook VSTO 加载项与外部 C++ 应用程序之间的交互

c# - 设计期间的 MarkupExtension.ProvideValue(IServiceProvider serviceProvider)

c# - 我可以在 C# 泛型约束中指定 'supertype' 关系吗?

c# - 如何通过 C# 代码更改用户帐户的密码?

c# - 仅在子项上带有复选框的 TreeView

wpf - 动画按钮背景颜色出错

c# - 如何将控件的属性绑定(bind)到另一个控件的属性?

ios - Xamarin形式FFImageLoading图像未显示

c# - 有没有在 XAML 中整合类似的数据绑定(bind)和/或触发器?

c# - WCF 服务是否公开属性?