Wpf 重置datagrid的非活动选择背景色
当焦点丢失时,是否有办法保留Wpf 重置datagrid的非活动选择背景色,wpf,xaml,datagrid,Wpf,Xaml,Datagrid,当焦点丢失时,是否有办法保留DataGrid中行的原始背景色 我知道InactiveSelectionHighlightBrushKey可以设置特定的颜色,但假设之前的背景颜色可以是红色或绿色,我希望InactiveSelectionHighlightBrushKey成为行的原始颜色 增加: 我在viewmodel中有IsZero属性,它是true/false,下面是 XAML: 其他行具有其他绑定以设置不同的颜色,但选择它并取消选择它将提供蓝色和灰色。我可以设置颜色,但不确定是否有一种优雅
DataGrid
中行的原始背景色
我知道InactiveSelectionHighlightBrushKey
可以设置特定的颜色,但假设之前的背景颜色可以是红色或绿色,我希望InactiveSelectionHighlightBrushKey
成为行的原始颜色
增加:
我在viewmodel中有IsZero属性,它是true/false,下面是
XAML:
其他行具有其他绑定以设置不同的颜色,但选择它并取消选择它将提供蓝色和灰色。我可以设置颜色,但不确定是否有一种优雅的方式可以将其设置为“当前背景色”的任何颜色。如果我理解正确,您可以使用本页中的答案:将
不活动选择HighlightBrushkey
设置为透明 使用InactiveSelectionHighlightBrushKey不是一个好的解决方案(至少对我来说),因为此属性已冻结且无法更改。
见:
试图修改它会导致运行时错误
在我的案例中,使用以下方法是有效的:
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
</DataGrid.Resources>
对于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>
如何将原始颜色设置为红色或绿色?请你发布一些XAML来显示你的情况…现场。我在其他任何地方都找不到答案。
<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>