Search code examples
wpfsilverlightwpfdatagrid

How to add back the sort arrow after applying background of Column headers


After applying background color to columnheaders, the sort arrow is missing. How to add it back?


Solution

  • I think you're gonna have to re-template the DataGridColumnHeader and add it from there. Here's an example. You're gonna have to add a reference to PresentationFramework.Aero

    xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    
    <DataGrid ...>
        <DataGrid.Resources>
            <Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
                <Setter Property="Width" Value="8"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="Cursor" Value="SizeWE"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Thumb}">
                            <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Background" Value="Blue"/>
                <Setter Property="BorderBrush" Value="Red"/>
                <Setter Property="BorderThickness" Value="1,1,1,1"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                            <Grid>
                                <Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <ContentPresenter Grid.Column="0" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                        <Path x:Name="SortArrow"
                                                Grid.Column="1"
                                                HorizontalAlignment="Right" VerticalAlignment="Center"                                           
                                                Width="8" Height="6" Margin="2,0,5,0"
                                                Stretch="Fill" Opacity="0.5" Fill="White"
                                                RenderTransformOrigin="0.5,0.4"
                                                Visibility="Collapsed"
                                                Data="M0,0 L1,0 0.5,1 z" />
                                    </Grid>
                                </Themes:DataGridHeaderBorder>
                                <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                                <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="SortDirection" Value="Ascending">
                                    <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                                    <Setter TargetName="SortArrow" Property="RenderTransform">
                                        <Setter.Value>
                                            <RotateTransform Angle="180" />
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                                <Trigger Property="SortDirection" Value="Descending">
                                    <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.Resources>
    </DataGrid>