不活动时 DataGrid 的选定行颜色

当 DataGrid 失去焦点时,如何样式化 WPF DataGrid 以更改选定行的颜色?

46442 次浏览

这样做:

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

自己找答案。

添加到 DataGrid 的资源中的画笔,它可以从代码后面更改其“ Color”属性,并引用 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>

然后添加 DataGrids 事件处理程序以手动更改颜色:

private void DataGrid1_LostFocus(object sender, RoutedEventArgs e)
{
((SolidColorBrush)DataGrid1.Resources["SelectionColorKey"]).Color = Colors.DarkGray;
}


private void DataGrid1_GotFocus(object sender, RoutedEventArgs e)
{
((SolidColorBrush)DataGrid1.Resources["SelectionColorKey"]).Color = SystemColors.HighlightColor;
}


private void DataGrid1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
((SolidColorBrush)DataGrid1.Resources["SelectionColorKey"]).Color = Colors.DarkGray;
}

经过多年的搜索,我发现了一个出人意料的简单方法,比之前发布的 Got/LostFocus 方法更简单:

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

这只是将不活动的背景颜色设置为暗灰色,将活动的背景颜色保留为默认颜色,但是您也可以使用 SystemColors 来改变这一点。当然还有 HighlightBrushKey。

非活动选择的前台资源键是 SystemColors。

这些答案都没有给我想要的答案。史蒂夫斯特里廷的顶级改变了其他部分的数据网格,我不想改变,其他答案不提供的 无效颜色变化,但正确的目标只有一行。所以这里是他们的答案的混合,改变的 无效颜色,只有的行,而不是在网格的其他地方。

<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。

<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>

Https://social.msdn.microsoft.com/forums/vstudio/en-us/635642e6-4808-4b3e-8aea-c8c434397d0f/datagrid-lost-focus-brush?forum=wpf

迟到的回答:

这在 。净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 中定义“ DataGrid. CellStyle”部分:

    <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));

对于 NET 4.5或更高版本,有以下选择来设置颜色 程序化:

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,其颜色分配如下: 背景和交替背景。 当 DataGrid 丢失焦点时,选定的行变为 Gray,当 DataGrid 通过单击任意行重新获得焦点时返回到正确的颜色。 这种效果令人不快。 我从史蒂夫 · 斯特里廷提出的解决方案开始:

<Style TargetType="DataGrid">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="LightGray"/>
</Style.Resources>
</Style>

而是通过改变颜色 = “透明”。

<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="Transparent"/>

这以一种简单而令人满意的方式解决了这个问题。 适用于 .net 4.5,不适用于 .net 4.0

希望这个解决方案有用

不正确的行为:

incorrect behavior

正确的行为:

correct behavior