Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change GridViewColumn header color

Tags:

listview

wpf

I have this ListView:

<ListView  Name="lvFiles" Background="Transparent" BorderThickness="0">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="File name" />
            <GridViewColumn Header="Size" Width="50" />
        </GridView>
    </ListView.View>
</ListView>

How can i set the Header color ?

like image 663
roz wer Avatar asked May 28 '15 11:05

roz wer


1 Answers

I have not used GridView personally, but a quick web search led me to:

GridView Column Header Styles and Templates Overview

That page links out to other pages to explain various things you can do with GridView column headers. It looks like there are multiple ways to set the "color" (not sure if you mean Foreground or Background), but using a Style seems like what you probably want. You can set a style to apply to all column headers like this:

<GridView>
    <GridView.ColumnHeaderContainerStyle>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <!-- Set any properties you want to set -->
            <Setter Property="Background" Value="LightBlue" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontWeight" Value="Bold" />
        </Style>
    </GridView.ColumnHeaderContainerStyle>
</GridView>

You can override the header style for a specific column like this:

<GridViewColumn Header="Something">
    <GridViewColumn.HeaderContainerStyle>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <!-- Set any properties you want to set -->
            <Setter Property="Background" Value="DarkBlue" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontWeight" Value="Bold" />
        </Style>
    </GridViewColumn.HeaderContainerStyle>
</GridViewColumn>

Edit: While you can customize most thing using styes and data templates (which are covered in the overview I linked at the top of this answer), there are certain things that are not customizable because they are baked into the control template – such as the border that appears above and below each column header. If you need to modify any of those things, then you will need to setup a style that sets the Template property to a custom ControlTemplate that you create.

If you want to make your own control template for a column header, I suggest you start by copying the example template into your application resources. Then tweak the parts you need to tweak. Keep in mind that setting the Template property on a control completely replaces the template, meaning you have to define the entire template that you want to use, not simply define parts that you want to be different from the default.

like image 137
Xavier Avatar answered Sep 19 '22 01:09

Xavier