【发布时间】:2013-01-30 08:05:45
【问题描述】:
我在 Windows 7 上使用 Visual Studio 2012。我需要知道为什么 Grid 所选行的以下样式不适用于背景和前景色,但适用于 BorderBrush 和 BorderThickness 等其他属性?虽然当鼠标悬停在网格行上时我可以看到它们发生变化。
<Style x:Key="gridRowStyle" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
<Setter Property="BorderBrush" Value="BlueViolet" />
<Setter Property="BorderThickness" Value="2" />
</Trigger>
</Style.Triggers>
</Style>
这是我在网格上的使用方式。
<DataGrid RowStyle="{StaticResource gridRowStyle}">
我强调要知道“为什么”而不是解决问题,因为如果我使用网格单元样式而不是行样式,我已经有了解决问题的方法,如下所示:
<Style x:Key="gridCellStyle" TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
</Trigger>
</Style.Triggers>
</Style>
【问题讨论】:
-
会不会是因为 GridCell 的内容覆盖了 GridRow?你试过在 Snoop 中查看控件吗?
-
我对您发布的内容进行了快速测试,效果很好。您能否提供有关 DataGrid 和任何 Row 定义的更多详细信息?
-
我是 WPF 新手,以前从未使用过 snoop。我会试试看发生了什么。谢谢....
-
@BrentStewart,这里是 DataGrid 的完整定义:
-
其中 RecordList 是简单类 Record 对象的自定义集合,在当前窗口资源中定义了两个公共属性 FirstName 和 LastName: public class Record { public string FirstName { set;得到; } 公共字符串姓氏 { 设置;得到; } } 公共类 MyCollection : ObservableCollection
{ }
标签: wpf datagrid background-color