0

I'm having a datagrid(WPF) with some items(ListViewCollection). I have one problem. I select one row, after that I sort by a column. The selected row changes it's background.

Below i have the datagrid code. And 2 screen pictures with before/after. You can see that the blue color turns in grey

<DataGrid Background="White" IsSynchronizedWithCurrentItem="{x:Null}"  EnableRowVirtualization="False"  AutoGenerateColumns="False" CanUserSortColumns="True" ItemsSource="{Binding ConsoleLines}" 
                               GridLinesVisibility="None" IsReadOnly="True" RowHeaderWidth="0" Style="{StaticResource DataGridStyle}" Margin="5">

                        <DataGrid.Columns>
                            <DataGridTemplateColumn CanUserSort="True" Header="{Binding TimeHeader, Source={StaticResource ResLoc}}" SortMemberPath="Time" Width="*">             
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Time}" TextWrapping="Wrap" Foreground="{StaticResource DarkBackgroundBrush}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn CanUserSort="True" Header="{Binding MessageHeader, Source={StaticResource ResLoc}}" SortMemberPath="Message" Width="*">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Message}" TextWrapping="Wrap" Foreground="{StaticResource DarkBackgroundBrush}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn CanUserSort="True" Header="{Binding CategoryHeader, Source={StaticResource ResLoc}}" SortMemberPath="Category" Width="150">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Path=Category}" TextWrapping="Wrap" Foreground="{StaticResource DarkBackgroundBrush}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>

Before

After

4

2 回答 2

1

After you applied the sort, the selected item colour was changed from HighlightBrushKey to InactiveSelectionHighLightBrushKey.

The best way to get the effect you want might be to copy this snippet into your user resources (or DataGrid resources) and select the colours you want for each of the given states...

<UserControl.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DeepSkyBlue"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="PowderBlue"/>
   <SolidColorBrush x:Key="{x:Static SystemColors.HotTrackBrushKey}" Color="CadetBlue"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="Plum"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Black"/>
</UserControl.Resources>
于 2013-07-10T13:23:34.470 回答
0

It's a focus issue ... when you click the column header to sort the row no longer has focus.

Using Snoop, when a row is clicked the Keyboard.FocusedElement is DataGridCell but when the column header is clicked it changes to HostWindow (at least on my apps).

Add the following and the row will have the same color in both cases:

        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridRow}">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DodgerBlue" />
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="DodgerBlue" />
                </Style.Resources>
            </Style>
        </DataGrid.Resources>

The downside it that after you click the column header, you'll have to click a row again if you want to navigate within the grid via Up, Down ... etc. keys. This may annoy the users.

于 2013-07-10T13:18:37.113 回答