[英]Styling Selected Cells in the WPF DataGrid Control
我试图更改WPF DataGrid控件的选定单元格的样式。
设置DataGridCell
和DataGridRow
样式有什么区别? 我在下面尝试了各种组合,它们都起作用。 然而,好像我只需要的款式无论是 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中的样式。
通常,我们指定后者来定义两个相邻像元之间的差异,例如指定像元之间的边距,边界等。
就像@abhishek所说的...我有一种情况,我需要同时使用行样式和单元格样式(这是我的行样式)
<!-- Row Style-->
<Style x:Key="RowStyle" TargetType="{x:Type dg:DataGridRow}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
然后我需要使用特定的单元格将其设为白色背景
<Style x:Key="TimeCell" TargetType="{x:Type dg:DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
并为特定的列指定样式
<dg:DataGridTemplateColumn Width="120" CellStyle="{StaticResource TimeCell}">
我希望我想说的很清楚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.