Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you style a WPF GridView Header?

I went from this: WPF GridViewHeader styling questions

to this:

WPF GridView Headers

Now I just need to get rid of the white space to the right of the "Size" header. I basically have a template for the GridViewColumnHeader that makes it a TextBlock. Is there any way I can set the background for that header area so that it spans the entire width of the GridView?

ADDED CODE:

This is my right-most column. The grid does not span 100% of available window area. In the header I need everything to the right of this column to have the same background as the column headers themselves.

<Style x:Key="GridHeaderRight" TargetType="{x:Type GridViewColumnHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                            <TextBlock Text="{TemplateBinding Content}" Padding="5" Width="{TemplateBinding Width}" TextAlignment="Right">
                                <TextBlock.Background>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                        <GradientStop Offset="0.0" Color="#373638" />
                                        <GradientStop Offset="1.0" Color="#77797B" />
                                    </LinearGradientBrush>
                                </TextBlock.Background>
                            </TextBlock>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="OverridesDefaultStyle" Value="True" />
                <Setter Property="Background" Value="Green" />
                <Setter Property="Foreground" Value="White" />
                <Setter Property="FontSize" Value="12" />
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <GradientStop Offset="0.0" Color="#373638" />
                            <GradientStop Offset="1.0" Color="#77797B" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Style>

<GridViewColumn Width="200" HeaderContainerStyle="{ StaticResource GridHeaderRight}" Header="Size">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=EmployeeNumber}" HorizontalAlignment="Right"></TextBlock>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

UPDATE

I am one step closer (I think) to solving this.

I added the following code inside the GridView tag:

<GridView.ColumnHeaderContainerStyle>
    <Style TargetType="GridViewColumnHeader">
        <Setter Property="BorderThickness" Value="1"></Setter>
        <Setter Property="BorderBrush" Value="Green"></Setter>
        <Setter Property="Height" Value="Auto"></Setter>
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Offset="0.0" Color="#373638" />
                    <GradientStop Offset="1.0" Color="#77797B" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
</GridView.ColumnHeaderContainerStyle>

The border is there just so you can see the boundary of what this style covers. This is an enlarged image of what this does. It seems to be what I want if I can get rid of the little white border on the bottom.

So I guess removing that tiny white bottom border would also be an accepted answer for this one.

like image 746
djschwartz Avatar asked Jul 28 '09 20:07

djschwartz


3 Answers

This is a simple style that will accomplish what you are looking for. Just change the Transparent background on the Border to be your desired gradient.

    <Style TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                    <Border BorderThickness="0,0,0,1" BorderBrush="Black" Background="Transparent">
                        <TextBlock x:Name="ContentHeader" Text="{TemplateBinding Content}" Padding="5,5,5,0" Width="{TemplateBinding Width}" TextAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="FontFamily" Value="Segoe UI" />
        <Setter Property="FontSize" Value="12" />
    </Style>
like image 146
Bret Faller Avatar answered Dec 18 '22 05:12

Bret Faller


sometimes the simplest way is the best. All you need to do it to change the TextBlock attached properties of GridViewColumnHeader

Define something like this in Resources:

<Style TargetType="{x:Type GridViewColumnHeader}" BasedOn="{StaticResource {x:Type GridViewColumnHeader}}">
    <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
    <Setter Property="TextBlock.Foreground" Value="Black"/>
</Style>
like image 35
Bizhan Avatar answered Dec 18 '22 05:12

Bizhan


Have a look at the GridViewColumnHeader.Role property. The sample in the documentation for the GridViewColumnHeaderRole enumeration might give you some ideas...

EDIT: Have you considered using the GridView.HeaderStyle property ?

like image 29
Thomas Levesque Avatar answered Dec 18 '22 06:12

Thomas Levesque