How to Assign ColumnHeaderContainerStyle and ColumnHeaderTemplate to a ListView Style

This is just a quick note on creating a ListView style with the appropriate GridView style and template assignments.

Normally, I’ve been creating listviews that look like this:

<ListView x:Name=”MyListView”
               ItemContainerStyle
=”{DynamicResource MyListViewItemContainerStyle}”>
   
<ListView.View>
        
<GridView ColumnHeaderContainerStyle=”{DynamicResource MyListViewHeaderStyle}”
                        
ColumnHeaderTemplate=”{DynamicResource MyGridColumnHeaderTemplate}”> 

I did this because I didn’t know exactly how to assign these styles and templates to the ListView Style. In the style, ColumnHeaderContainerStyle and ColumnHeaderTemplate are not properties of the ListView, they are properties of the GridView… which you can’t create a style for.

Instead, you can encapsulate all the information above in the following style.

<Style x:Key=”CustomListViewStyle” TargetType=”{x:Type ListView}”>
      <
Setter Property=”GridView.ColumnHeaderContainerStyle” Value=”{DynamicResource MyListViewHeaderStyle}” />
     
<Setter Property=”GridView.ColumnHeaderTemplate” Value=”{DynamicResource MyGridColumnHeaderTemplate}” />
     
<Setter Property=”ItemContainerStyle” Value=”{DynamicResource MyListViewItemContainerStyle}” />
</Style>

Problem solved.

4 Comments

  1. Nick:

    I’ve tried your solution but only the ItemContainerStyle applied not the HeaderTemplate!

    I would appreciate any help you can give me!

    – Window XAML –

    – ResourceDictionary –

  2. Sonia:

    You have some very useful xaml on this site but I’m still struggling with styling the ListView. In this post you reference a {DynamicResource MyListViewItemContainerStyle}. What does that look like. I can set an ItemContainer style within an individual list view with this sort of thing

    but I don’t know how to define it separately.

    Can you help?

    Thanks much,
    Sonia

  3. Sonia:

    The code that I didn’t paste into the first post :

  4. Sonia:

    It’s not showing up – last try:

Leave a comment