DataGrid's selected row color when inactive

前端 未结 11 1345
轻奢々
轻奢々 2020-12-04 23:31

How can I style WPF DataGrid to change the color of selected row when DataGrid lost its focus?

11条回答
  •  我在风中等你
    2020-12-04 23:53

    Find an answer by myself.

    Add to DataGrid's resources the brush, which can change its 'Color' property from code behind, and reference HighlightBrushKey to it:

    
        
        
    
    

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

提交回复
热议问题