C# WPF DataGrid-即使SelectedItem是绑定属性,也高亮显示选定行
我有一个WPF数据网格,其中SelectedItem绑定到ViewModel属性C# WPF DataGrid-即使SelectedItem是绑定属性,也高亮显示选定行,c#,wpf,datagrid,C#,Wpf,Datagrid,我有一个WPF数据网格,其中SelectedItem绑定到ViewModel属性 SelectedItem="{Binding DataContext.SelectedBooking, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 如果用户单击一行并选择它,唯一的视觉线索是该行的灰色背景会变得非常轻微。我需要使这一点更加明显,因此我尝试单独添加以下内容: <DataGr
SelectedItem="{Binding DataContext.SelectedBooking, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
如果用户单击一行并选择它,唯一的视觉线索是该行的灰色背景会变得非常轻微。我需要使这一点更加明显,因此我尝试单独添加以下内容:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
及
结果是一样的。当用户单击某一行时,该行会短暂地闪烁红色,然后变回浅灰色,尽管该行实际上仍处于选中状态。如果他们第二次点击它,它会变红并保持红色
如果我删除SelectedItem上的绑定,它将按预期工作。如何在不考虑绑定的情况下实现这一点?我自己找到了答案,在Intellisense中滚动查看SystemColor。还有一个你也可以覆盖的笔刷
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
</DataGrid.Resources>
一个有趣的小技巧是:
这将使不活动设置为活动,无论默认设置或必须更改高亮键。不活动选择HighlightBrushKey仅适用于FW 4.5,并在FW 4.0中引发异常。请查收
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
</DataGrid.Resources>