Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove gradient style that lies behind the headers

Tags:

wpf

xaml

I have the following screen at the moment, I would like to remove the two-tone background that lies behind the headers, the red arrow points to where it is and the green highlights the area that it covers.

enter image description here

Under normal circumstances you would not see it, however because I have textboxes in column 5 stretching the column header it is appearing.

I would like to remove this, making it transparent, but I do not know the component on the DataGrid or how to access it. Can someone help me?

The following web page has a brilliant diagram of the components http://blog.smoura.com/wpf-toolkit-datagrid-part-ii-custom-styling/ It shows the same sort of area defined as a DataGridColumnHeadersPresenter, I am not sure if this is the one I should be aiming at or not. I have given it a shot without much luck. enter image description here

Here is my full XAML

<UserControl x:Class="Users.View.AllUsersView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
             mc:Ignorable="d" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:helpers="clr-namespace:Users.ViewModel.Helpers"
             d:DesignHeight="160" d:DesignWidth="1100">

    <UserControl.Resources>
        <CollectionViewSource x:Key="UserCollection" Source="{Binding Path=AllUsers}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Name" Direction="Ascending" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

        <Style x:Key="UserItemStyle" TargetType="{x:Type DataGridRow}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="ItemsControl.AlternationIndex" Value="1" />
                        <Condition Property="IsSelected" Value="False" />
                        <Condition Property="IsMouseOver" Value="False" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" Value="#EEEEEEEE" />
                </MultiTrigger>
            </Style.Triggers>
        </Style>

        <DataTemplate x:Key="headerTemplate">
            <TextBlock Padding="0,0,0,0" Margin="0,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Bottom" Text="{Binding}"/>
        </DataTemplate>

        <Style x:Key="RowHeaderStyle2" TargetType="DataGridRowsPresenter">
            <Setter Property="Background" Value="{x:Null}"/>
        </Style>
        <Style x:Key="ColumnHeaderStyle" TargetType="DataGridColumnHeader">
            <Setter Property="VerticalContentAlignment" Value="Bottom"/>
            <Setter Property="VerticalAlignment" Value="Bottom"/>
            <Setter Property="Height" Value="25"/>
        </Style>
        <Style x:Key="ModuleColumnHeaderStyle" TargetType="GridViewColumnHeader">
            <Setter Property="VerticalContentAlignment" Value="Bottom"/>
            <Setter Property="Width" Value="550"/>
        </Style>

        <helpers:FirstItemConverter x:Key="firstItemConvertion"/>

        <Style x:Key="ListViewItemRotatedText" TargetType="TextBlock">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <RotateTransform Angle="-45" />
                </Setter.Value>
            </Setter>
            <Setter Property="Width" Value="130"/>
            <Setter Property="Padding" Value="0,0,0,0"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ModuleName, Converter={StaticResource firstItemConvertion}, ConverterParameter=0}" Value="true">
                        <Setter Property="Margin" Value="-60,0,0,0" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <DockPanel>
        <Grid DockPanel.Dock="Bottom" Margin="0,2,4,2">
            <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" VerticalAlignment="Center">
                <TextBlock Text="Selected Users: " />
                <ContentPresenter Content="{Binding Path=TotalSelectedUsers}" ContentStringFormat="0" />
                <TextBlock Text=" of " />
                <ContentPresenter Content="{Binding Path=TotalUsers}" ContentStringFormat="0" />
            </StackPanel>
        </Grid>
        <DataGrid AutoGenerateColumns="False" ItemContainerStyle="{StaticResource UserItemStyle}" AlternatingRowBackground="{x:Null}" DataContext="{StaticResource UserCollection}" ItemsSource="{Binding}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Name}"/>
                <DataGridTextColumn Header="Job Title" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Job_Title}"/>
                <DataGridTextColumn Header="Department" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Department}"/>
                <DataGridTextColumn Header="Company" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Company}"/>
                <DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=Company}" CanUserResize="False" Width="580">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                            <TextBlock HorizontalAlignment="Center">Modules</TextBlock>
                            <ListBox x:Name="lstModules" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.Modules}">
                                <ListBox.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" CanVerticallyScroll="False" CanHorizontallyScroll="False"></StackPanel>
                                    </ItemsPanelTemplate>
                                </ListBox.ItemsPanel>
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Style="{StaticResource ListViewItemRotatedText}" Text="{Binding ModuleName}"></TextBlock>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Header="Contact Detail" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Name}"/>
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
</UserControl>

[EDIT] After placing background color on the DataGridColumnHeader's

<Style x:Key="ColumnHeaderStyle" TargetType="DataGridColumnHeader">
    <Setter Property="VerticalContentAlignment" Value="Bottom"/>
    <Setter Property="VerticalAlignment" Value="Bottom"/>
    <Setter Property="Height" Value="25"/>
    <Setter Property="Background" Value="Tomato"/>
</Style>
<Style x:Key="ModuleColumnHeaderStyle" TargetType="DataGridColumnHeader">
    <Setter Property="Background" Value="Tomato"/>
</Style>

This is what it looks like: as you can see the white/grey two-tone color is still there. enter image description here

[EDIT2] To get the same in the fifth column add in these styles

<Style x:Key="ListViewItemRotatedText1" TargetType="TextBlock">
    <Setter Property="LayoutTransform">
        <Setter.Value>
            <RotateTransform Angle="-45" />
        </Setter.Value>
    </Setter>
    <Setter Property="Width" Value="130"/>
    <Setter Property="Margin" Value="0,0,0,0" />
</Style>
<Style x:Key="ListViewItemRotatedText2" TargetType="TextBlock">
    <Setter Property="LayoutTransform">
        <Setter.Value>
            <RotateTransform Angle="-45" />
        </Setter.Value>
    </Setter>
    <Setter Property="Width" Value="130"/>
    <Setter Property="Margin" Value="-60,0,0,0" />
</Style>

And replace ... with the following StackPanel

<StackPanel Orientation="Horizontal">
    <TextBlock Style="{StaticResource ListViewItemRotatedText1}" >Customer Services</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Asset Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Works Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Project Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Rates Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Finance</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Human Resources</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Document Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >User Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Configuration</TextBlock>
</StackPanel>

[LAST EDIT] Result with Viv's help enter image description here

like image 508
Hank Avatar asked Oct 16 '25 08:10

Hank


1 Answers

Update:

<Style TargetType="DataGridColumnHeader">
  <Setter Property="Background"
          Value="Tomato" />
</Style>

^^ don't specify a x:Key we are trying to let this reach the dummy Header(PART_FillerColumnHeader) which supposedly spans all Columns and gives a Background.

We'd also need to get ColumnHeaderStyle to inherit this so we'll end up having,

<Style TargetType="DataGridColumnHeader">
  <Setter Property="Background"
          Value="Tomato" />
</Style>
<Style x:Key="ColumnHeaderStyle"
        BasedOn="{StaticResource {x:Type DataGridColumnHeader}}"
        TargetType="DataGridColumnHeader">
  <Setter Property="VerticalContentAlignment"
          Value="Bottom" />
  <Setter Property="VerticalAlignment"
          Value="Bottom" />
  <Setter Property="Height"
          Value="25" />
</Style>
like image 74
Viv Avatar answered Oct 18 '25 14:10

Viv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!