DataGrid的选定行颜色处于非活动状态时
当DataGrid失去焦点时,如何deviseWPF DataGrid来更改所选行的颜色?
经过多年的search之后,我发现了一个非常简单的方法,比之前发布的Got / LostFocus方法更清洁:
<DataGrid.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DarkGray"/> </DataGrid.Resources>
这只是将非活动的背景颜色设置为DarkGray,将活动的背景颜色设置为默认值,但是当然也可以使用SystemColors.HighlightBrushKey更改。
非活动select的前景资源键是SystemColors.InactiveSelectionHighlightTextBrushKey。
完整的解决scheme,适用于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>
像这样做:
<DataGrid ...> <DataGrid.Resources> <Style TargetType="DataGridRow"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/> </Style.Resources> </Style> </DataGrid.Resources> ...
对于.Net Framework 4.0(或者如果您不想使用InactiveSelection …刷键):创build一个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>
自己找一个答案。
向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; }
这些答案都没有给我相当我正在寻找的东西。 史蒂夫街头最高评价改变了我不想改变的数据网格的其他部分,其他答案没有提供不活动的颜色变化,但只是正确定位行。 所以这里是他们的答案的混合物,改变非活动的颜色, 只在行上,而不是在网格上的其他地方。
<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
<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>
你应该像这样在你的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>
我将其添加到我的ResourceDictionary,以便它适用于我的程序中的所有数据网格。
<Style TargetType="DataGrid"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="LightGray"/> </Style.Resources> </Style>