DataGrid's selected row color when inactive

前端 未结 11 1300
轻奢々
轻奢々 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-05 00:06

    None of these answers gave me quite what I was looking for. The top rated by Steve Streeting changed other sections of the datagrid that I didn't want to change, and other answers weren't providing the inactive color change, but were properly targeting the row only. So here's a mixture of their answers that changes the inactive color, only on the rows and not in other places on the grid.

    <DataGrid.Resources>
        <Style TargetType="DataGridRow">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DarkGray"/>
            </Style.Resources>
        </Style>
    </DataGrid.Resources>
    
    0 讨论(0)
  • 2020-12-05 00:07

    For .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>
    

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/635642e6-4808-4b3e-8aea-c8c434397d0f/datagrid-lost-focus-brush?forum=wpf

    0 讨论(0)
  • 2020-12-05 00:10

    For .NET 4.0 or higher: It is also possible to set the colors programmatically:

    if (TestDataGrid.RowStyle == null)
    {
      TestDataGrid.RowStyle = new Style(typeof(DataGridRow));
    }
    
    // Set colors for the selected rows if focus is inactive
    TestDataGrid.RowStyle.Resources.Add(SystemColors.ControlBrushKey, new SolidColorBrush(Colors.SkyBlue));
    TestDataGrid.RowStyle.Resources.Add(SystemColors.ControlTextBrushKey, new SolidColorBrush(Colors.Black));
    
    // Set colors for the selected rows if focus is active
    TestDataGrid.RowStyle.Resources.Add(SystemColors.HighlightBrushKey, new SolidColorBrush(Colors.Red));
    TestDataGrid.RowStyle.Resources.Add(SystemColors.HighlightTextBrushKey, new SolidColorBrush(Colors.White));
    

    For .NET 4.5 or higher there is the following alternative to set the colors programmatically:

    if (TestDataGrid.Resources == null)
    {
      TestDataGrid.Resources = new ResourceDictionary();
    }
    
    // Set colors for the selected rows if focus is inactive
    TestDataGrid.Resources.Add(SystemColors.InactiveSelectionHighlightBrushKey, new SolidColorBrush(Colors.SkyBlue));
    TestDataGrid.Resources.Add(SystemColors.InactiveSelectionHighlightTextBrushKey, new SolidColorBrush(Colors.Black));
    
    // Set colors for the selected rows if focus is active
    TestDataGrid.Resources.Add(SystemColors.HighlightBrushKey, new SolidColorBrush(Colors.Red));
    TestDataGrid.Resources.Add(SystemColors.HighlightTextBrushKey, new SolidColorBrush(Colors.White));
    
    0 讨论(0)
  • 2020-12-05 00:13

    Complete solution that works for 4.0. Note that this on the 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>
    
    0 讨论(0)
  • 2020-12-05 00:14

    You should define section "DataGrid.CellStyle" inside your DataGrid like this:

        <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>
    
    0 讨论(0)
提交回复
热议问题