Wpf 数据网格&x27;不活动时选择的行颜色
当DataGrid失去焦点时,如何设置WPF DataGrid的样式以更改所选行的颜色?如下操作:Wpf 数据网格&x27;不活动时选择的行颜色,wpf,wpfdatagrid,Wpf,Wpfdatagrid,当DataGrid失去焦点时,如何设置WPF DataGrid的样式以更改所选行的颜色?如下操作: <DataGrid ...> <DataGrid.Resources> <Style TargetType="DataGridRow"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemCol
<DataGrid ...>
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
</Style.Resources>
</Style>
</DataGrid.Resources>
...
<DataGrid>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
...
自己找答案
将画笔添加到DataGrid的资源中,它可以从代码隐藏更改其“颜色”属性,并引用HighlightBrushKey:
<DataGrid.Resources>
<SolidColorBrush x:Key="SelectionColorKey" Color="DarkGray"/>
<Style TargetType="DataGridRow">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding Source={StaticResource SelectionColorKey}, Path=Color}"/>
</Style.Resources>
</Style>
</DataGrid.Resources>
经过多年的搜索,我发现了一种令人惊讶的简单方法,比之前发布的get/LostFocus方法更简洁:
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DarkGray"/>
</DataGrid.Resources>
这只是将非活动背景颜色设置为DarkGray,将活动背景颜色保留为默认颜色,但您也可以使用SystemColor.HighlightBrushKey更改该颜色
非活动选择的前景资源键是SystemColor.InactiveSelectionHighlightTextBrushKey。这些答案都没有给出我想要的答案。史蒂夫·斯特雷廷(Steve Streeting)的最高评分改变了我不想改变的数据网格的其他部分,其他答案没有提供非活动的颜色变化,但只针对行。所以这里是他们答案的混合,它改变了非活动的颜色,只在行上,而不是在网格上的其他地方
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DarkGray"/>
</Style.Resources>
</Style>
</DataGrid.Resources>
适用于.Net Framework 4.0(或者如果您不想使用InactiveSelection…笔刷键):
创建一个DataGridRow样式/控件模板,并添加以下触发器:
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="{DynamicResource SelectionBrush}" />
</Trigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=IsKeyboardFocusWithin}" Value="False" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="{DynamicResource InactiveSelectionBrush}" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</ControlTemplate.Triggers>
适用于4.0的完整解决方案。请注意,这是在CellStyle上
<DataGrid.CellStyle>
<!--Override Highlighting so that its easy to see what is selected even when the control is not focused-->
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=IsKeyboardFocusWithin}" Value="False" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
适用于.net Framework 4.0
<Style TargetType="DataGridRow">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="DarkGray" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="White"/>
</Style.Resources>
</Style>
迟到的回答:
这适用于.Net 4.0,您不必硬编码颜色:
<Style TargetType="DataGridRow">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="{x:Static SystemColors.HighlightTextColor}"/>
</Style.Resources>
</Style>
我将其添加到我的ResourceDictionary中,以便它适用于我程序中的所有数据网格
<Style TargetType="DataGrid">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="LightGray"/>
</Style.Resources>
</Style>
您应该在数据网格中定义“DataGrid.CellStyle”部分,如下所示:
<DataGrid ...>
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
</Style.Resources>
</Style>
</DataGrid.Resources>
...
<DataGrid>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
.NET 4.0或更高版本的:也可以通过编程设置颜色:
if (TestDataGrid.RowStyle == null)
{
TestDataGrid.RowStyle = new Style(typeof(DataGridRow));
}
// Set colors for the selected rows if focus is inactive
TestDataGrid.RowStyle.Resources.Add(SystemColors.ControlBrushKey, new SolidColorBrush(Colors.SkyBlue));
TestDataGrid.RowStyle.Resources.Add(SystemColors.ControlTextBrushKey, new SolidColorBrush(Colors.Black));
// Set colors for the selected rows if focus is active
TestDataGrid.RowStyle.Resources.Add(SystemColors.HighlightBrushKey, new SolidColorBrush(Colors.Red));
TestDataGrid.RowStyle.Resources.Add(SystemColors.HighlightTextBrushKey, new SolidColorBrush(Colors.White));
if (TestDataGrid.Resources == null)
{
TestDataGrid.Resources = new ResourceDictionary();
}
// Set colors for the selected rows if focus is inactive
TestDataGrid.Resources.Add(SystemColors.InactiveSelectionHighlightBrushKey, new SolidColorBrush(Colors.SkyBlue));
TestDataGrid.Resources.Add(SystemColors.InactiveSelectionHighlightTextBrushKey, new SolidColorBrush(Colors.Black));
// Set colors for the selected rows if focus is active
TestDataGrid.Resources.Add(SystemColors.HighlightBrushKey, new SolidColorBrush(Colors.Red));
TestDataGrid.Resources.Add(SystemColors.HighlightTextBrushKey, new SolidColorBrush(Colors.White));
对于.NET 4.5或更高版本,可以使用以下替代方法以编程方式设置颜色:
if (TestDataGrid.RowStyle == null)
{
TestDataGrid.RowStyle = new Style(typeof(DataGridRow));
}
// Set colors for the selected rows if focus is inactive
TestDataGrid.RowStyle.Resources.Add(SystemColors.ControlBrushKey, new SolidColorBrush(Colors.SkyBlue));
TestDataGrid.RowStyle.Resources.Add(SystemColors.ControlTextBrushKey, new SolidColorBrush(Colors.Black));
// Set colors for the selected rows if focus is active
TestDataGrid.RowStyle.Resources.Add(SystemColors.HighlightBrushKey, new SolidColorBrush(Colors.Red));
TestDataGrid.RowStyle.Resources.Add(SystemColors.HighlightTextBrushKey, new SolidColorBrush(Colors.White));
if (TestDataGrid.Resources == null)
{
TestDataGrid.Resources = new ResourceDictionary();
}
// Set colors for the selected rows if focus is inactive
TestDataGrid.Resources.Add(SystemColors.InactiveSelectionHighlightBrushKey, new SolidColorBrush(Colors.SkyBlue));
TestDataGrid.Resources.Add(SystemColors.InactiveSelectionHighlightTextBrushKey, new SolidColorBrush(Colors.Black));
// Set colors for the selected rows if focus is active
TestDataGrid.Resources.Add(SystemColors.HighlightBrushKey, new SolidColorBrush(Colors.Red));
TestDataGrid.Resources.Add(SystemColors.HighlightTextBrushKey, new SolidColorBrush(Colors.White));
我也有类似的问题。
该场景是一个DataGrid,其颜色指定为:
RowBackground和AlternatingRowBackground。
当DataGrid失去焦点时,所选行变为灰色,当DataGrid通过单击任何行重新获得焦点时,会返回到正确的颜色。
这种效果令人不快。
我从Steve Streeting在此提出的解决方案开始:
但通过改变颜色=“透明”
这以一种简单而令人满意的方式解决了这个问题。
适用于.net 4.5
,不适用于.net 4.0
希望这个解决方案有用
错误行为:
正确的行为:
谢谢你的回答!它会更改选择颜色。但也适用于主动聚焦案例。因此,行始终为红色,无论是活动行还是非活动行。我还需要另一个行为:当DataGrid有焦点时,蓝色选中行,当DataGrid没有焦点时,红色选中行。@HCL为什么我们需要为SystemColor键指定一个新值,而不是像通常那样给背景另一种颜色?是因为DataGrid覆盖了代码中的背景色吗?或者这与我们的价值来自静态环境这一事实有关不更正文本颜色InactiveSelectionHighlightBrushKey仅适用于FW 4.5,并引发FW 4.0异常。我用.NET 4.5写了更多关于这一点的文章。我使用以下代码:
为什么我们需要为SystemColor键指定一个新值,而不是像通常那样为背景提供另一种颜色?是因为DataGrid覆盖了代码中的背景色吗?还是与我们的值来自静态上下文这一事实有关?InactiveSelectionHighlightBrushKey在.net 4.0中不可用:(您可能必须对DataGrid.CellStyle而不是行执行此操作。我仍然不清楚何时使用每种样式。好的,这段代码是在a内还是在a内。一个完整的、有效的答案会很有用。但为什么它会这样工作?为什么选择了row触发器还不够?