重置数据网格的非活动选择背景色



有没有办法在焦点丢失时保留DataGrid行的原始背景颜色?

我知道InactiveSelectionHighlightBrushKey可以设置特定的颜色,但是假设以前的背景颜色可以是红色或绿色,我希望InactiveSelectionHighlightBrushKey行的原始颜色是什么?

添加:

我在视图模型中有 IsZero 属性,它是真/假的,下面是XAML:

          <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsZero}" Value="True">
                            <Setter Property="Background" Value="Green" />
                        </DataTrigger>
其他

行具有其他绑定来设置不同的颜色,但选择它并取消选择它会得到蓝色和灰色。我可以设置颜色,但不确定如何优雅地将其设置为"当前背景颜色"。

如果我

理解正确,您可以使用此页面中的答案:DataGrid在非活动时选择的行颜色,以将InactiveSelectionHighlightBrushKey设置为Transparent

使用 InactiveSelectionHighlightBrushKey 不是一个好的解决方案(至少对我来说),因为此属性被冻结并且无法更改。请参阅:http://msdn.microsoft.com/en-us/library/system.windows.systemcolors.inactiveselectionhighlightbrushkey.aspx

尝试修改它会导致我出现运行时错误。

在我的情况下,使用以下方法有效:

<DataGrid.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>           
</DataGrid.Resources>
<</div> div class="one_answers">

对于 WPF 4.0,我发现以下内容效果最好,因为在其他解决方案中,当我输入另一个数据网格时,行仍然变为灰色。

<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
        </Trigger>          
    </Style.Triggers>
</Style>

最新更新