WPF DataGrid失去焦点后行选择丢失

5
我遇到了一个与DataGrid非常奇怪的行为。我在DataGridRow上有以下触发器。
<Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{StaticResource SelectionBackgroundBrush}"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

一开始当行被选择时,我从上面的触发器中得到了该行为。然而,在选择后,如果DataGrid失去焦点(例如我在窗口上单击其他按钮),前景属性就会失去其值,但背景仍按触发器中指定的方式保留。是否有人遇到过这种情况,或者我的上述代码(或应用程序中的其他地方)存在问题。有没有解决以上问题的方法?

1个回答

0

我使用了DataGridCell而不是DataGridRow,这对我有效

    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <Trigger Property="DataGridCell.IsSelected" Value="True">
                <Setter Property="BorderBrush" Value="#CCDAFF" />
                <Setter Property="Background" Value="#3399ff"/>
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
    </Style>

希望能对某些人有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接