WPF is powerful. so powerful in fact, that sometimes it's hard to find Styles and settings to make it do what you want. aligning listview items was one such example for me. this snippet tutorial will show you how to use a style to vertically and horizontally align the contents of listview cells.
I Came authentication ss the need to vertically align the contents of my listview when I had columns that contained elements that were different heights. by default, the listview vertically aligns all of the content to the middle. I wanted them aligned to the top.
Here's a quick application I threw together to demonstrate what I'm talking about. it has a listview bound to a collection of videogame objects. each video game contains the title and an image of the box art. first, the object and some code to populate it:
Public class videogame
{
Public string name
{
Get;
Set;
}
Public String Image
{
Get;
Set;
}
}
...
Public partial class window1: Window
{
Private observablecollection <videogame> _ games =
New observablecollection <videogame> ();
Public observablecollection <videogame> games
{
Get {return _ games ;}
}
Public window1 ()
{
_ Games. Add (new videogame (){
Name = "crysis ",
Image = @ "C: \ crysis_boxart_final.jpg "});
_ Games. Add (new videogame (){
Name = "Unreal Tournament 3 ",
Image = @ "C: \ gearsofwar. jpg "});
_ Games. Add (new videogame (){
Name = "Gears of War ",
Image = @ "C: \ crysis_boxart_final.jpg "});
Initializecomponent ();
}
}
And here's the XAML that displays this information:
<Window X: class = "verticalignsnippet. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1" Height = "512" width = "512" name = "mywindow">
<Grid>
<Listview name = "mylistview"
Itemssource = "{binding elementname = mywindow, Path = games}">
<Listview. View>
<Gridview>
<Gridviewcolumn header = "title" displaymemberbinding = "{binding name}"/>
<Gridviewcolumn header = "image">
<Gridviewcolumn. celltemplate>
<Datatemplate>
<Grid>
<Image Source = "{binding image}"/>
</GRID>
</Datatemplate>
</Gridviewcolumn. celltemplate>
</Gridviewcolumn>
</Gridview>
</Listview. View>
</Listview>
</GRID>
</WINDOW>
When you launch this application, you'll get something that looks like this:
As you can see, each title is centered vertically to the image. I want to align it to the top. Here's the style needed to accomplish this:
<Listview. itemcontainerstyle>
<Style targettype = "listviewitem">
<Setter property = "verticalcontentalignment" value = "TOP"/>
</Style>
</Listview. itemcontainerstyle>
Now if we slap that into our listview, We'll get exactly what we're re looking:
<Window X: class = "verticalignsnippet. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1" Height = "512" width = "512" name = "mywindow">
<Grid>
<Listview name = "mylistview"
Itemssource = "{binding elementname = mywindow, Path = games}">
<Listview. itemcontainerstyle>
<Style targettype = "listviewitem">
<Setter property = "verticalcontentalignment" value = "TOP"/>
</Style>
</Listview. itemcontainerstyle>
<Listview. View>
<Gridview>
<Gridviewcolumn header = "title" displaymemberbinding = "{binding name}"/>
<Gridviewcolumn header = "image">
<Gridviewcolumn. celltemplate>
<Datatemplate>
<Grid>
<Image Source = "{binding image}"/>
</GRID>
</Datatemplate>
</Gridviewcolumn. celltemplate>
</Gridviewcolumn>
</Gridview>
</Listview. View>
</Listview>
</GRID>
</WINDOW>
We can also use this style to horizontally align the content. let's say I wanted to align all of the titles to the right as well as the top. all we 'd have to do is change our style to this:
<Listview. itemcontainerstyle>
<Style targettype = "listviewitem">
<Setter property = "verticalcontentalignment" value = "TOP"/>
<Setter property = "horizontalcontentalignment" value = "right"/>
</Style>
</Listview. itemcontainerstyle>