WPF DataGrid - 即使SelectedItem是绑定属性,也要突出显示所选行

时间:2014-11-20 17:05:53

标签: c# wpf datagrid

我有一个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>

结果是一样的。当用户单击一行时,它会非常短暂地闪烁红色,然后返回到淡灰色,尽管Row实际上仍然是被选中。如果他们第二次点击它,它会变红并保持红色。

如果我删除SelectedItem上的Binding,它会按预期工作。无论绑定如何,我怎样才能完成这项工作?

1 个答案:

答案 0 :(得分:7)

我自己找到了答案,滚动了SystemColors的Intellisense。还有一个InactiveSelection画笔也可以覆盖。

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