C# 为什么有两种不同类型的DataGridRow高亮显示颜色,为什么';我不能控制他们两个吗?
我在WPF中有一个DataGrid,其SelectedIndex字段绑定到viewmodel中的相应属性。当窗口打开时,我可以看到选中的行与未选中的行相比呈微弱的灰色阴影。如果用户单击该行,该行将变为蓝色 这是一个问题的原因,因为我似乎只能设置被用户单击高亮显示的行的背景色,而不能设置被编程更改的选定索引的背景色。我尝试过以下类型的样式,但它们只影响单击行的背景颜色,而不影响SelectedIndex设置的颜色: 例1:C# 为什么有两种不同类型的DataGridRow高亮显示颜色,为什么';我不能控制他们两个吗?,c#,wpf,datagrid,C#,Wpf,Datagrid,我在WPF中有一个DataGrid,其SelectedIndex字段绑定到viewmodel中的相应属性。当窗口打开时,我可以看到选中的行与未选中的行相比呈微弱的灰色阴影。如果用户单击该行,该行将变为蓝色 这是一个问题的原因,因为我似乎只能设置被用户单击高亮显示的行的背景色,而不能设置被编程更改的选定索引的背景色。我尝试过以下类型的样式,但它们只影响单击行的背景颜色,而不影响SelectedIndex设置的颜色: 例1: <DataGrid.RowStyle>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}" >
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
例2:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}" >
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
</DataGrid.RowStyle>
那么,如何设置所选索引的高亮度颜色?因为我的下一步是忽略用户点击。不管怎么说,这两种类型的行选择,用户单击的类型和selectedindex以编程方式设置的类型之间有什么区别?有两种选择样式,因为选择可以是活动的,也可以是非活动的(当
element.IsFocused==false
时)
试试这个:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}" >
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused" Value="False"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Blue"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
然而,由于视觉上的区别是有原因的,我建议你改为深入研究。例如,您可能希望在最初显示网格或更改其选择时聚焦网格。解决方案:
//C#, WPF
dataGridInstance.Focus(); //make sure DataGrid is focused
dataGridInstance.SelectAll(); //select a row programmatically
以下是我最初用于搜索此解决方案的问题:
- 为什么在WPF DataGrid中以编程方式选择行会导致 灰色高亮而不是蓝色李>
- 为什么要通过代码在WPF DataGrid中选择行 与鼠标单击相比,高亮显示的效果有什么不同