WPF 数据网格 - 突出显示“选定项”行,即使“选定项”是绑定属性

本文关键字:选定项 即使 属性 绑定 数据 数据网 显示 WPF 网格 | 更新日期: 2023-09-27 18:31:10

>我有一个WPF DataGrid,其中SelectedItem绑定到ViewModel属性。

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>

<DataGrid.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
</DataGrid.Resources>

结果是一样的。当用户单击某一行时,它会非常短暂地闪烁红色,然后恢复为浅灰色,尽管该行实际上仍处于"已选择"状态。如果他们再次点击它,它会变成红色并保持红色。

如果我删除选定项上的绑定,它会按预期工作。无论绑定如何,我如何才能使这项工作?

WPF 数据网格 - 突出显示“选定项”行,即使“选定项”是绑定属性

我自己找到了答案,滚动浏览了SystemColors的Intellisense。您也可以覆盖一个非活动选择画笔。

<DataGrid.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
</DataGrid.Resources>