本文介绍了非活动时 DataGrid 的选定行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当 DataGrid 失去焦点时,如何设置 WPF DataGrid 的样式以更改所选行的颜色?
How can I style WPF DataGrid to change the color of selected row when DataGrid lost its focus?
推荐答案
自己找答案.
将画笔添加到 DataGrid 的资源中,它可以从后面的代码中更改其颜色"属性,并将 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 的选定行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!