Styling The ListView

Taken from this blog post, this is so important that I figured to give it it’s own page so it can remain at the top view.

A quick note: I will be dealing almost entirely with the listview look. If you want the listview to do something (functionality) or look a certain way and you can’t find the answer here, leave a comment with your suggestion and I’ll try to blog about it and place a link to the answer in my:

ListView FAQ

My goal is to create a significant repository on getting the stinking listview to look the way you want and do what you want it to do.

You’re probably here because, compared with most of the WPF controls, Blend gives very little guidance on how to deal with the listview (even though you use the listview for practically everything you do). So we’re going to start with the basic structure of the listview. This is what the basic listview looks like in the XAML.

<ListView>
     
<ListView.View>
            <GridView>
                 
<GridViewColumn Header=”Column Header/>
           
</GridView>
     
</ListView.View>
</ListView>

So this post will start out giving basic guidance on what to edit when you’re trying to edit the various parts of the listview. I will update these sections with links and tutorials on listview specific tasks as time goes on.

As a point of note, I recommend using an ItemSource for your listview when you’re learning ot style it. Manually putting ListViewItems into it and trying to style them will muddy the waters of good design. If you need a good ItemsSource, I recommend any of the New York Times RSS feeds.

How to connect the New York Times RSS feed to your listview

Question 1: “How do I make the items (see picture below) look the way I want them to?”

ListView_ItemContainerStyle

Answer: Use the ItemContainerStyle property in the ListView (and the associated ControlTemplate)

More on ItemContainerStyle and Blend

In the XAML:

Put this in the resources:
<Style x:Key=”MyItemContainerStyleTargetType=”{x:Type ListViewItem}>
</Style>

Put this in the composition:
<ListView ItemContainerStyle=”{DynamicResource MyItemContainerStyle}>

Question 2: “How do I make all the items in a certain column (see below) look a certain way?”

LV_CellTemplate

Answer: Use a CellTemplate in the GridViewColumn

More on CellTemplate and Blend.

In the XAML:

Put this in the resources:
<DataTemplate x:Key=”MyCellTemplate>
</DataTemplate>

Put this in the composition:
<GridViewColumn CellTemplate=”{DynamicResource MyCellTemplate}>

Question 3: “How do I make the whole header (see below) look the way I want?”

ListView_ColumnHeaderContainerStyle

Answer: Use the ColumnHeaderContainerStyle in the GridView (and the associated ControlTemplate)

More on the ColumnHeaderContainerStyle and Blend

In the XAML:

Put this in the resources:
<Style x:Key=”MyColumnHeaderContainerStyleTargetType=”{x:Type GridViewColumnHeader}>
</Style>

Put this in the composition:
<GridView ColumnHeaderContainerStyle=”{DynamicResource MyColumnHeaderContainerStyle}>

Question 4: “How do I change the layout of the header sections (all of them)?”

ListView_ColumnHeaderTemplate

Answer: Use a ColumnHeaderTemplate in the GridView

How do I get to the ColumnHeaderTemplate using Blend? (coming soon)

In the XAML:

Put this in the resources:
<DataTemplate x:Key=”MyColumnHeaderTemplate>
</DataTemplate>

Put this in the composition:
<GridView ColumnHeaderTemplate=”{DynamicResource MyColumnHeaderTemplate}>

Question 5: “How do I make a specific part of the header look different than another part?”
or “How do I make the red part look different than the blue part?”

ListView_ColumnHeaderTemplate_better

Answer: Use the HeaderTemplate in the GridViewColumn

How do I get to the HeaderTemplate using Blend? (coming soon)

In the XAML:

Put this in the resources:
<DataTemplate x:Key=”MyHeaderTemplate>
</DataTemplate>

Put this in the composition:
<GridViewColumn HeaderTemplate=”{DynamicResource MyHeaderTemplate}>

6 Comments

  1. De Tran:

    This page is so beneficial to me. I keep come back to it to see what maps to what (probably the blue/red rectangles -:). Thank you very much.

    Just a small recommendation for the sake of completeness of the page, if you can tell readers
    1) the shortcut to skin horz/vertical scroll bars which are part of the visible listview.
    2) when horz and vert scrollbar shown, there is a small square in lower right cornor

    Have a great day and keep doing the good work,
    Thanks,
    -De

  2. jason:

    I think in #3 above, {x:Type GridViewContainerHeader} should actually be {x:Type GridViewColumnHeader}.
    (Thanks again for this page. I use it far too often.)

  3. Ronnie:

    Thanks, I’ve been looking for documentation on this
    How do you hide columns in the listview. For example, I’d like to show a list of students names and hide their student id number and bus route number

  4. ChloeLim:

    Thanks for the effort on listing out all these useful information.
    How to add in an image in front of each row? Like a “tick” for active record, and a “cross” for lock record.
    Sorry for poor description. I hope you can understand my question

  5. Shaun:

    Hi Mattias. Where did you find the style (i.e. colors and gradients) for your list view? Did you create this yourself?

  6. Matthias:

    Shaun, Most of the styles I use are default styles in Vista. I get a code copy of them by selecting the ListView in Blend and going to the “Objects” menu and select “Edit a copy…” from any of the “Edit Style/Edit Control Template” submenus.

Leave a comment