非活动时DataGrid的选定行颜色

编程入门 行业动态 更新时间:2024-10-27 11:26:47
本文介绍了非活动时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的选定行颜色

本文发布于:2023-11-11 10:12:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578135.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!