本文介绍了非活动时DataGrid的选定行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当DataGrid失去焦点时,如何设置WPF DataGrid的样式以更改所选行的颜色?
How can I style WPF DataGrid to change the color of selected row when DataGrid lost its focus?
推荐答案
查找答案
将画笔添加到DataGrid的资源中,可以从后面的代码中更改其 Color属性,并对其进行引用HighlightBrushKey:
Add to DataGrid's resources the brush, which can change its 'Color' property from code behind, and reference HighlightBrushKey to it:
<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事件处理程序以手动更改颜色:
Then add DataGrids event handlers to manually change the color:
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的选定行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!