This quick article will enable you to take full advantage of the ListView control by creating a simple data model and through a step-by-step incremental approach. I will try to be concise and direct in order for you to keep this article as future development reference.
Lets start by creating a new WPF Window and define a base data model for our sample. We’ll create a ListView control named “ListView”:
|
1 |
<ListView Name="ListView" ItemsSource="{Binding}" /> |
Window and data context setup:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public partial class ListViewStylingWindow : Window { public ListViewStylingWindow() { InitializeComponent(); ListView.DataContext = new ObservableCollection<Person>() { new Person() {Name = "Peter", Age = 22, FavoriteMovie = "Transformers"}, new Person() {Name = "Anne", Age = 19, FavoriteMovie = "Lord of the Rings"}, new Person() {Name = "John", Age = 25, FavoriteMovie = "Titanic"} }; } } public class Person { public string Name { get; set; } public int Age { get; set; } public string FavoriteMovie { get; set; } } |
I’ve added an ObservableCollection with initialized 3 Person class objects to the Data Context of the control. Now let’s configure the ListView layout to accommodate the data structure we’re passing into the control’s DataContext. For this example, the GridView view mode of the ListView will be used.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<ListView Name="ListView" ItemsSource="{Binding}"> <ListView.View> <GridView> <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" /> <GridViewColumn Header="Age"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Age}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="Favorite Movie"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Path=FavoriteMovie}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> |
Notice that in the Age and Movie columns, I’ve purposefully used a CellTemplate definition to show you cell data templating. This is what we have so far:
So this will be the base from which we will start styling. We will follow a step-by-step styling procedure for easier reference.
Step #1: ListView Column Header styling (GridViewColumn)
We’ll start by adding a few brush styles to the resources collection and then reference it in the column style. Remember that GridView columns are derived from ButtonBase, so you have access to all the behaviors derived from it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<LinearGradientBrush x:Key="BackgroundBrush" StartPoint="0,0" EndPoint="0,1"> <LinearGradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="#FF6FBDE8" Offset="0"/> <GradientStop Color="#FF4385BE" Offset="1"/> </GradientStopCollection> </LinearGradientBrush.GradientStops> </LinearGradientBrush> <LinearGradientBrush x:Key="HighlightBackgroundBrush" StartPoint="0,0" EndPoint="0,1"> <LinearGradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="#FF97d3f3" Offset="0"/> <GradientStop Color="#FF4385BE" Offset="1"/> </GradientStopCollection> </LinearGradientBrush.GradientStops> </LinearGradientBrush> <LinearGradientBrush x:Key="BorderBrush" StartPoint="0,0" EndPoint="0,1"> <LinearGradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="#FFAFDDF6" Offset="0"/> <GradientStop Color="#FF2969AA" Offset="1"/> </GradientStopCollection> </LinearGradientBrush.GradientStops> </LinearGradientBrush> <LinearGradientBrush x:Key="PressedBorderBrush" StartPoint="0,0" EndPoint="0,1"> <LinearGradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="#FF75aac7" Offset="0"/> <GradientStop Color="#FF143c65" Offset="1"/> </GradientStopCollection> </LinearGradientBrush.GradientStops> </LinearGradientBrush> |
Before we add the final header style, we’ll first add the gripper style. This will allow us to visually define the portion of the header that will respond to column resize and dragging operations.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<Style x:Key="GridViewColumnHeaderGripper" TargetType="Thumb"> <Setter Property="Width" Value="18"/> <Setter Property="Background" Value="#2e566b"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Thumb}"> <Border Padding="{TemplateBinding Padding}" Background="Transparent"> <Rectangle HorizontalAlignment="Center" Width="1" Fill="{TemplateBinding Background}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> |
This will add a nice touch to the UI. Now well add the style for the GridViewColumn. This style will aggregate previous resources and provide visual responses for mouse over, press and drag events:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<Style x:Key="{x:Type GridViewColumnHeader}" TargetType="GridViewColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Foreground" Value="#FFFFFF" /> <Setter Property="Padding" Value="8"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="GridViewColumnHeader"> <Grid> <Border Name="HeaderBorder" Padding="{TemplateBinding Padding}" BorderThickness="0,1,0,1" BorderBrush="{StaticResource BorderBrush}" Background="{StaticResource BackgroundBrush}"> <ContentPresenter Name="HeaderContent" Margin="0,0,0,1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Border> <Thumb x:Name="PART_HeaderGripper" HorizontalAlignment="Right" Margin="0,0,-9,0" Style="{StaticResource GridViewColumnHeaderGripper}"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="HeaderBorder" Property="Background" Value="{StaticResource HighlightBackgroundBrush}"/> </Trigger> <Trigger Property="IsPressed" Value="true"> <Setter TargetName="HeaderBorder" Property="Background" Value="{StaticResource PressedBorderBrush}"/> <Setter TargetName="HeaderContent" Property="Margin" Value="1,1,0,0"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="Role" Value="Floating"> <Setter Property="Opacity" Value="0.7"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="GridViewColumnHeader"> <Canvas Name="PART_FloatingHeaderCanvas"> <Rectangle Fill="#60000000" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}"/> </Canvas> </ControlTemplate> </Setter.Value> </Setter> </Trigger> <Trigger Property="Role" Value="Padding"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="GridViewColumnHeader"> <Border Name="HeaderBorder" BorderThickness="0,1,0,1" BorderBrush="{StaticResource BorderBrush}" Background="{StaticResource BackgroundBrush}"/> </ControlTemplate> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> |
And this is the outcome:
This sums up the first part of this ListView styling tutorial. In the [link2post id="633"]next article[/link2post], I’ll be covering in-depth styling on ListView items. For now, you can download the full sample solution here.





















Good tutorial, txh.
Better if you post your codes package.
Updated article with solution sample download.
Great tutorial, thank you!
hi great tutorial
how to access the subitems in that list?
any ideas please
Can you detail a bit further please?
Thanks a lot for this tutorial.
You’re welcome!