<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Veracity Solutions Blog &#187; GridViewColumnHeader</title>
	<atom:link href="http://blogs.veracitysolutions.com/tag/gridviewcolumnheader/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.veracitysolutions.com</link>
	<description>Software Consulting That Rocks</description>
	<lastBuildDate>Thu, 22 Apr 2010 17:21:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Embedded ListView Columns (Columns Within Columns)</title>
		<link>http://blogs.veracitysolutions.com/embedded-listview-columns-columns-within-columns/</link>
		<comments>http://blogs.veracitysolutions.com/embedded-listview-columns-columns-within-columns/#comments</comments>
		<pubDate>Sat, 26 Jan 2008 03:58:08 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[ListView]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[advanced]]></category>
		<category><![CDATA[columns]]></category>
		<category><![CDATA[Data Template]]></category>
		<category><![CDATA[embedded columns]]></category>
		<category><![CDATA[GridViewColumnHeader]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.designerwpf.com/2008/01/25/embedded-listview-columns-columns-within-columns/</guid>
		<description><![CDATA[Please Read: Strangely, when you do a Google search for &#8220;wpf&#8221; and &#8220;listview&#8221;,Â this is one of theÂ top links. This is odd because this particular post isÂ kind of an advanced tutorial. If you&#8217;re looking for more general information on styling the wpf listview, check out this post. It is probably much closer to what you&#8217;re looking [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Please Read:</strong> Strangely, when you do a Google search for &#8220;wpf&#8221; and &#8220;listview&#8221;,Â this is one of theÂ top links. This is odd because this particular post isÂ kind of an advanced tutorial. If you&#8217;re looking for more general information on styling the wpf listview, check out <a href="http://www.designerwpf.com/2007/12/06/the-wpf-designers-guide-to-styling-the-your-favorite-adjectival-swear-word-here-listview/">this post</a>. It is probably much closer to what you&#8217;re looking for.</p>
<p>This is a bit of an advanced tutorial. I&#8217;m putting it up because I just figured out how to do it and I want to share. You can also <a href="http://www.designerwpf.com/wp-content/uploads/2008/01/listviewembeddedcolumns.zip" title="Embedded Columns Project Files">download the project files for this tutorial</a>Â (in zip format&#8230; requires .Net 3.5).</p>
<p>Recently, I received from my user experience designers a wireframe that looked something like this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/embededwireframe.png" alt="EmbeddedWireframe" /></p>
<p>As you can see, there are embedded categories (categories within categories) here. I considered many solutions (hacks), but I found that a deeper understanding of the ListView and how it works would allow me to resolve this issue very simply (and without even touching the code behind).<span id="more-165"></span></p>
<h2>&lt;Exposition&gt;</h2>
<p>Soâ€¦ within every ListView, there is a Gridview that holds a set of GridViewColumns. What I didn&#8217;t realize until a couple days ago was that when you put the GridViewColumns into the GridView, you&#8217;re implicitly handing the GridView a GridViewColumnCollection.This collection is then referenced by the GridViewHeaderRowPresenter (located deep inside the ListView Template) and the GridViewRowPresenter, which is located inside the ListViewItem template (which I usually get to through the ItemContainerStyleâ€¦ see here for more details).</p>
<p>Anyway, these two presenters databind back up to the GridView to grab the GridViewColumns that we&#8217;ve defined.</p>
<p>Once I understood that, I realized that I could create a GridViewColumnCollection as a resource and the reference it inside some custom templates.</p>
<p>At this point, I get the feeling that some readers are just hearing &#8220;blah blah blah&#8221;. It&#8217;s actually easier to demonstrate than it is to explain. So let&#8217;s get to it.</p>
<h2>&lt;/Exposition&gt;</h2>
<p>We&#8217;re going to grab the header image from the New York Times RSS feed and display it in its own column while displaying all the accompanying header data under a broad Images header with sub-columns called Title, URL, and Link. It will look like this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/embededfinal.png" alt="EmbeddedFinalScreen" /></p>
<h2>Resources</h2>
<p>First, lets deal with all the resources we need. Take note that this part will be pretty XAML heavy.</p>
<p>Create a resources section. If mine isn&#8217;t in a separate resource dictionary, I usually just create it at the top of the window as seen below.</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">Window.Resources</font><font color="#0000ff">&gt;</font><br />
<font color="#0000ff">&lt;/</font><font color="#800000">Window.Resources</font><font color="#0000ff">&gt;</font></p>
<p>All your resources stuff will go in between those two tags.Â </p>
<p>Create a GridViewColumnCollection in the resources:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">GridViewColumnCollection</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">ImageColumnCollection</font>&#8220;<font color="#0000ff">&gt;<br />
Â Â Â Â Â  &lt;</font><font color="#800000">GridViewColumn </font><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Title</font>&#8221; <font color="#0000ff">/&gt;<br />
Â Â Â Â Â  </font><font color="#0000ff">&lt;</font><font color="#800000">GridViewColumn </font><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">URL</font>&#8221; <font color="#0000ff">/&gt;<br />
</font><font color="#0000ff"><font color="#000000">Â Â Â Â Â  </font>&lt;</font><font color="#800000">GridViewColumn</font> <font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Link</font>&#8221; <font color="#0000ff">/&gt;<br />
</font><font color="#0000ff">&lt;/</font><font color="#800000">GridViewColumnCollection</font><font color="#0000ff">&gt;</font></p>
<p>Now, go through the motions to getting your RSS feed. Follow the steps I lay out in <a href="http://www.designerwpf.com/2007/12/13/how-do-i-connect-an-rss-feed-to-my-listviewlistboxwhatever/">my post on getting the New York Times RSS feeds</a>, except that, instead of clicking on the <strong>item(Array)</strong>, click on the <strong>image </strong>section. Continue on with the <strong>&#8220;Create Data Template&#8221;</strong> option to get your RSS bindings automated.</p>
<p>Grab the bindings in this template and put them into the DisplayMemberBinding property in the GridViewColumns. Your GridViewColumnCollection should now look like this:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">GridViewColumnCollection</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">ImageColumnCollection</font>&#8220;<font color="#0000ff">&gt;<br />
Â Â Â Â Â  &lt;</font><font color="#800000">GridViewColumn </font><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Title</font>&#8221;<br />
Â Â Â Â Â Â Â Â Â Â Â  <font color="#ff0000">DisplayMemberBinding</font>=&#8221;<font color="#b8860b">{Binding Mode=OneWay, XPath=title}</font>&#8220;Â Â <font color="#0000ff">/&gt;<br />
Â Â Â Â Â  </font><font color="#0000ff">&lt;</font><font color="#800000">GridViewColumn </font><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">URL</font>&#8221;<br />
Â Â Â Â Â Â Â Â Â Â Â  <font color="#ff0000">DisplayMemberBinding</font>=&#8221;<font color="#b8860b">{Binding Mode=OneWay, XPath=url}</font>&#8220;Â Â Â <font color="#0000ff">/&gt;<br />
</font><font color="#0000ff"><font color="#000000">Â Â Â Â Â  </font>&lt;</font><font color="#800000">GridViewColumn</font> <font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Link</font>&#8221;<br />
Â Â Â Â Â Â Â Â Â Â Â  <font color="#ff0000">DisplayMemberBinding</font>=&#8221;<font color="#b8860b">{Binding Mode=OneWay, XPath=link}</font>&#8220;Â Â Â <font color="#0000ff">/&gt;<br />
</font><font color="#0000ff">&lt;/</font><font color="#800000">GridViewColumnCollection</font><font color="#0000ff">&gt;</font></p>
<p>Now that we have our GridViewColumnCollection in place, let&#8217;s create some stuff that will actually use it.</p>
<p>Create a new empty Data Template. If you&#8217;re still uncomfortable digging into the XAML itself, you can just type the following in:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">DataTemplate</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">MultiColumnHeaderTemplate</font>&#8220;<font color="#0000ff">&gt;</font><br />
<font color="#0000ff">&lt;/</font><font color="#800000">DataTemplate</font><font color="#0000ff">&gt;</font></p>
<p>And then go to your resources tab and double click on it to make it accessible in the design mode.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/embededresources.png" alt="ResourcesTab" /></p>
<p>The way I set this up was very simple. I put in a grid with two rows set to auto, put a TextBlock in the top one and a GridViewHeaderRowPresenter into the other one. The TextBlock at the top is bound to the Header Content with a simple binding:</p>
<p><font color="#ff0000">Text</font>=&#8221;<font color="#b8860b">{Binding}</font>&#8221;</p>
<p>The real trick is that the GridViewHeaderRowPresenter is bound to our Columns resource by simply entering the following property:</p>
<p><font color="#ff0000">Columns</font>=&#8221;<font color="#b8860b">{DynamicResource ImageColumnCollection}</font>&#8221;</p>
<p>Now, each column in that collection will send its header information to be handled by this particular GridViewHeaderRowPresenter.</p>
<p>Our final header data template looks something like this:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">DataTemplate</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">MultiColumnHeaderTemplate</font>&#8220;<font color="#0000ff">&gt;<br />
Â Â Â Â Â  &lt;</font><font color="#800000">Grid</font><font color="#0000ff">&gt;<br />
Â Â Â Â Â Â Â Â Â Â Â  &lt;</font><font color="#800000">Grid.RowDefinitions</font><font color="#0000ff">&gt;<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &lt;</font><font color="#800000">RowDefinition</font> <font color="#ff0000">Height</font>=&#8221;<font color="#0000ff">Auto</font>&#8221; <font color="#0000ff">/&gt;<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  &lt;</font><font color="#800000">RowDefinition</font> <font color="#ff0000">Height</font>=&#8221;<font color="#0000ff">Auto</font>&#8221; <font color="#0000ff">/&gt;<br />
Â Â Â Â Â Â Â Â Â Â Â  &lt;/</font><font color="#800000">Grid.RowDefinitions</font><font color="#0000ff">&gt;<br />
Â Â Â Â Â Â Â Â Â Â Â  &lt;</font><font color="#800000">TextBlock</font> <font color="#ff0000">Text</font>=&#8221;<font color="#b8860b">{Binding}</font>&#8221;<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  <font color="#ff0000">HorizontalAlignment</font>=&#8221;<font color="#0000ff">Center</font>&#8220;<font color="#0000ff">/&gt;<br />
Â Â Â Â Â Â Â Â Â Â Â  &lt;</font><font color="#800000">GridViewHeaderRowPresenter</font> <font color="#ff0000">Grid.Row</font>=&#8221;<font color="#0000ff">1</font>&#8221;<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  Â <font color="#ff0000">HorizontalAlignment</font>=&#8221;<font color="#0000ff">Stretch</font>&#8221;<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  <font color="#ff0000">Columns</font>=&#8221;<font color="#b8860b">{DynamicResource ImageColumnCollection}</font>&#8221; <font color="#0000ff">/&gt;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â<br />
Â Â Â Â Â  &lt;/</font><font color="#800000">Grid</font><font color="#0000ff">&gt;<br />
&lt;/</font><font color="#800000">DataTemplate</font><font color="#0000ff">&gt;</font></p>
<p>That takes care of our headersâ€¦ what about our item data? How do I get this data to fit into the proper columns? This was a problem I struggled with for quite some time until a friend of mine pointed out that the GridViewRowPresenter in the ItemTemplate could be extracted and used where ever I wanted to use it. Resultingly, I could place it into the CellTemplate of my Image Information column to compliment the placement of the GridViewHeaderRowPresenter in the Header of that same column.</p>
<p>If you&#8217;re following along and trying hard to learn, create a new DataTemplate and just toss a GridViewRowPresenter into it, binding it to the same column collection. Or you can just copy and paste the XAML below. Your choice.</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">DataTemplate</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">MultiColumnCellTemplate</font>&#8220;<font color="#0000ff">&gt;<br />
Â Â Â Â Â  &lt;</font><font color="#800000">Grid</font><font color="#0000ff">&gt;<br />
</font><font color="#0000ff">Â Â Â Â Â Â Â Â Â Â Â  &lt;</font><font color="#800000">GridViewRowPresenter</font>Â <font color="#ff0000">HorizontalAlignment</font>=&#8221;<font color="#0000ff">Stretch</font>&#8221;<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  <font color="#ff0000">Columns</font>=&#8221;<font color="#b8860b">{DynamicResource ImageColumnCollection}</font>&#8221; <font color="#0000ff">/&gt;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â<br />
Â Â Â Â Â  &lt;/</font><font color="#800000">Grid</font><font color="#0000ff">&gt;<br />
&lt;/</font><font color="#800000">DataTemplate</font><font color="#0000ff">&gt;</font>Â </p>
<p>This will be the basis of our embedded column design. We&#8217;re almost there, let&#8217;s just tie up some resources loose ends.</p>
<p>In order to get the image to show up in the &#8220;Image&#8221; column, we need to create a handy little DataTemplate to display that image. Simply have an image in the Data Template and have the Source property point to the url binding for the image like so:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">DataTemplate</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">ImageTemplate</font>&#8220;<font color="#0000ff">&gt;<br />
Â Â Â Â Â Â </font><font color="#0000ff">&lt;</font><font color="#800000">Image</font>Â <font color="#ff0000">Source</font>=&#8221;<font color="#b8860b">{Binding Mode=OneWay, XPath=url}</font>&#8221; <font color="#0000ff">/&gt;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â<br />
</font><font color="#0000ff">&lt;/</font><font color="#800000">DataTemplate</font><font color="#0000ff">&gt;</font>Â </p>
<p>OKâ€¦ we are now ready to actually do something outside of our resources. Don&#8217;t worry, this will be fast.</p>
<h2>Main XAML Composition</h2>
<p>If you haven&#8217;t already drawn a ListView in your main window, do so now.Â  Right click on the ListView andÂ  go to <strong>Bind ItemSource to Dataâ€¦</strong></p>
<p>Â <img src="http://www.designerwpf.com/wp-content/uploads/2007/12/itemsourcemenu.png" alt="ItemSourceMenu" /></p>
<p>In the resulting pop-up choose your RSS Feed (mine is called NYTTech) and select the image option from it.Â  (If you haven&#8217;t added the RSS feed, just copy and paste the line below to the top of your resources.)</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">XmlDataProvider</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">NYTTech</font>&#8221;<br />
Â Â Â Â Â  <font color="#ff0000">d:IsDataSource</font>=&#8221;<font color="#0000ff">True</font>&#8221;<br />
Â Â Â Â Â  <font color="#ff0000">Source</font>=&#8221;<font color="#0000ff">http://www.nytimes.com/services/xml/rss/nyt/Technology.xml</font>&#8221; <font color="#0000ff">/&gt;</font></p>
<p>Now, in the interest of time, we&#8217;re going to go right into the XAML. If you&#8217;re interested in seeing how to do this using Blend only, <a href="http://www.designerwpf.com/2007/12/13/styling-a-listview-column-using-blend/">check this post out</a>. Trust me, it&#8217;s not worth it.</p>
<p>Because we&#8217;ve already constructed most of what we need in the resources, we only need to do a couple things here. First, create two GridViewColumns, one with</p>
<p><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Image</font>&#8220;Â </p>
<p>and another with</p>
<p><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Image Information</font>&#8221;</p>
<p>In the Image column, set</p>
<p><font color="#ff0000">CellTemplate</font>=&#8221;{StaticResource ImageTemplate}&#8221;</p>
<p>In the Image Information column (which is our column with embedded columns), we just need to point to our already constructed templates like so:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">GridViewColumnHeader <font color="#ff0000">Header<font color="#000000">=&#8221;</font><font color="#0000ff">Image Information</font><font color="#000000">&#8220;</font>Â<br />
Â Â Â Â Â  HeaderTemplate<font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource MultiColumnHeaderTemplate}</font><font color="#000000">&#8220;Â<br />
Â Â Â Â Â Â </font></font></font><font color="#0000ff"><font color="#800000"><font color="#ff0000">CellTemplate</font><font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource MultiColumnCellTemplate}</font><font color="#000000">&#8221; </font></font><font color="#0000ff">&gt;</font></font></p>
<p>Go ahead and run the project and you&#8217;ll see that we have one tiny little problem left.</p>
<p>Because the default content alignment of the header style is to center everything, all of our columns are misaligned. To solve this problem, copy the following style into your resources:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">Style </font><font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">StretchHeaderStyle</font>&#8221; <font color="#ff0000">TargetType</font>=&#8221;<font color="#b8860b">{x:Type GridViewColumnHeader}</font>&#8220;<font color="#0000ff">&gt;<br />
Â Â Â Â Â  &lt;</font><font color="#800000">Setter</font><font color="#ff0000"> Property</font>=&#8221;<font color="#0000ff">HorizontalContentAlignment</font>&#8221; <font color="#ff0000">Value</font>=&#8221;<font color="#0000ff">Stretch</font>&#8220;<font color="#0000ff">/&gt;<br />
&lt;/</font><font color="#800000">Style</font><font color="#0000ff">&gt;</font></p>
<p>And point to it in your ListView GridView with a :</p>
<p><font color="#ff0000">ColumnHeaderContainerStyle</font>=&#8221;<font color="#b8860b">{StaticResource StretchHeaderStyle}</font>&#8221;</p>
<p>At the end, your ListView should look like this:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">ListView</font> <font color="#ff0000">IsSynchonizedWithCurrentItem</font>=&#8221;<font color="#0000ff">True</font>&#8220;<font color="#0000ff">&gt;<br />
Â Â Â Â Â  &lt;</font><font color="#800000">ListView.View</font><font color="#0000ff">&gt;<br />
Â Â Â Â Â Â Â Â Â Â Â  &lt;</font><font color="#800000">GridView <font color="#ff0000">ColumnHeaderContainerStyle</font><font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource StretchHeaderStyle}</font><font color="#000000">&#8221; </font></font><font color="#0000ff">&gt;<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &lt;<font color="#800000">GridViewColumn <font color="#ff0000">Header<font color="#000000">=&#8221;</font><font color="#0000ff">Image</font><font color="#000000">&#8220;</font><br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  </font></font></font><font color="#0000ff"><font color="#800000"><font color="#ff0000">CellTemplate</font><font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource ImageTemplate}</font><font color="#000000">&#8221; </font></font><font color="#0000ff">&gt;<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  &lt;<font color="#800000">GridViewColumn <font color="#ff0000">Header<font color="#000000">=&#8221;</font><font color="#0000ff">Image Information</font><font color="#000000">&#8220;</font>Â<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  HeaderTemplate<font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource MultiColumnHeaderTemplate}</font><font color="#000000">&#8221;<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  </font></font></font><font color="#0000ff"><font color="#800000"><font color="#ff0000">CellTemplate</font><font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource MultiColumnCellTemplate}</font><font color="#000000">&#8221; </font></font><font color="#0000ff">&gt;</font></font><font color="#0000ff">Â<br />
Â Â Â Â Â Â Â Â Â Â Â  </font></font></font><font color="#0000ff">&lt;/</font><font color="#800000">GridView</font><font color="#0000ff">&gt;<br />
Â Â Â Â Â  </font><font color="#0000ff">&lt;<font color="#800000">ListView.View</font><font color="#0000ff">&gt;</font><br />
&lt;/</font><font color="#800000">ListView</font><font color="#0000ff">&gt;</font></p>
<p>And if you hate writing XAML, you notice with a grateful heart that I&#8217;ve posted every bit of XAML you need right here. Additionally (and this is a first for this site), because of the complexity of this post, I&#8217;m offering this project as a downloadable zipÂ file so you can tweak it or futz with it at your convenience.</p>
<p><a href="http://www.designerwpf.com/wp-content/uploads/2008/01/listviewembeddedcolumns.zip" title="Embedded Columns Project Files">Embedded Columns Project Files</a>Â (in zip format&#8230; requires .Net 3.5)</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/embedded-listview-columns-columns-within-columns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Styling the ListView Column Header</title>
		<link>http://blogs.veracitysolutions.com/styling-the-listview-column-header/</link>
		<comments>http://blogs.veracitysolutions.com/styling-the-listview-column-header/#comments</comments>
		<pubDate>Thu, 17 Jan 2008 06:37:12 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Blend]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[ListView]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[column header]]></category>
		<category><![CDATA[columnheadercontainerstyle]]></category>
		<category><![CDATA[GridViewColumnHeader]]></category>
		<category><![CDATA[GridViewHeaderRowPresenter]]></category>
		<category><![CDATA[listview header]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.designerwpf.com/2008/01/16/styling-the-listview-column-header/</guid>
		<description><![CDATA[ListView header Styling is one of the most difficult styling pieces I&#8217;ve had to deal with. Part of this is because it is just another part of the seemingly endlessly complex listview. The other part is just because of the way the styling for the listview is put together in WPF.
In this post, we&#8217;re going [...]]]></description>
			<content:encoded><![CDATA[<p>ListView header Styling is one of the most difficult styling pieces I&#8217;ve had to deal with. Part of this is because it is just another part of the seemingly endlessly complex listview. The other part is just because of the way the styling for the listview is put together in WPF.</p>
<p>In this post, we&#8217;re going to change the default color of the header (background and foreground) and make the headers look more like bubbles. Why? Because we can!Â (Everytime I say that, somewhere aÂ usabilityÂ expert loses a little bit of their soul.)</p>
<p>Take note that anything done in this will affect the <em>whole header</em>. If you&#8217;re looking to do something to one individual column in the header, you need to go to this post on ColumnHeaders (coming soon). See the bottom of this post for more details.</p>
<p>As a point of note, the easy way in this particular case involves going directly into the XAML and the hard way involves going through the steps in Blend. The easy way is posted at the bottom.</p>
<p>Now for the hard way. First, go to your listview, right click on it and go to:</p>
<p><strong>Edit Control Parts (Template) -&gt; Edit a Copy&#8230;</strong></p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/1_controlparts.png" alt="1_ControlParts" /><br />
<span id="more-149"></span><br />
You&#8217;ll be given the basic template of your listview. Right-click on the scrollviewer and go to:</p>
<p><strong>Edit Control Parts (Template) -&gt; Edit Template</strong></p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/2_controltemplate2.png" alt="2_ControlTemplate" /></p>
<p>You&#8217;ll be given the default scrollViewer template for the specialized scrollViewer used in the ListView. In addition to the normal scrollviewer parts, it has a control that acts as the holder for the header called the &#8220;GridViewHeaderRowPresenter&#8221;.Â </p>
<p>Highlight the GridViewHeaderRowPresenter (which I am renaming &#8220;steve&#8221; so I don&#8217;t have to write that anymore) and, in the menu, go to:</p>
<p><strong>Object -&gt; Edit Other Styles -&gt; Edit ColumnHeaderContainerStyle -&gt; Edit a Copyâ€¦</strong></p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/3_columnheadercontainerstyle.png" alt="3_ColumnHeaderContainerStyle" /></p>
<p>This is where we are going to want to changeÂ the foreground and background.</p>
<p>Go over to the Properties panel to the right and click on foreground, giving it a solid color brush (I&#8217;m going to use a dark blue). Do the same thing with the background (light blue)and the BorderBrush (blue gradient) (you&#8217;ll see why in a moment).</p>
<p>Now my style looks like this in the XAML:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">Style</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">CustomHeaderStyle</font>&#8221; <font color="#ff0000">TargetType</font>=&#8221;<font color="#cb860b">{x:Type GridViewColumnHeader}</font>&#8220;<font color="#0000ff">&gt;</font><br />
Â Â Â Â Â  <font color="#0000ff">&lt;</font><font color="#800000">Setter</font> <font color="#ff0000">Property</font>=&#8221;<font color="#0000ff">Background</font>&#8221; <font color="#ff0000">Value</font>=&#8221;<font color="#0000ff">#FFCDE7F4</font>&#8221; <font color="#0000ff">/&gt;</font><br />
Â Â Â Â Â  <font color="#0000ff">&lt;</font><font color="#800000">Setter</font> <font color="#ff0000">Property</font>=&#8221;<font color="#0000ff">Foreground</font>&#8221; <font color="#ff0000">Value</font>=&#8221;<font color="#0000ff">#FF07034B</font>&#8220;<font color="#0000ff">/&gt;</font><br />
Â Â Â Â Â  <font color="#0000ff">&lt;</font><font color="#800000">Setter</font> <font color="#ff0000">Property</font>=&#8221;<font color="#0000ff">BorderBrush</font>&#8220;<font color="#0000ff">&gt;</font><br />
Â Â Â Â Â Â Â Â Â Â Â  <font color="#0000ff">&lt;</font><font color="#800000">Setter.Value</font>&gt;<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  <font color="#0000ff">&lt;</font><font color="#800000">LinearGradientBrush</font> <font color="#ff0000">EndPoint</font>=&#8221;<font color="#0000ff">0.5,1</font>&#8221; <font color="#ff0000">StartPoint</font>=&#8221;<font color="#0000ff">0.5,0</font>&#8220;<font color="#0000ff">&gt;</font><br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  <font color="#0000ff">&lt;</font><font color="#800000">GradientStop</font> <font color="#ff0000">Color</font>=&#8221;<font color="#0000ff">#FFA1CCE0</font>&#8221; <font color="#ff0000">Offset</font>=&#8221;<font color="#0000ff">0</font>&#8220;<font color="#0000ff">/&gt;</font><br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  <font color="#0000ff">&lt;</font><font color="#800000">GradientStop</font> <font color="#ff0000">Color</font>=&#8221;<font color="#0000ff">#FFABD5EA</font>&#8221; <font color="#ff0000">Offset</font>=&#8221;<font color="#0000ff">1.0</font>&#8220;<font color="#0000ff">/&gt;</font><br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  <font color="#0000ff">&lt;/</font><font color="#800000">LinearGradientBrush</font><font color="#0000ff">&gt;</font><br />
Â Â Â Â Â Â Â Â Â Â Â  <font color="#0000ff">&lt;/</font><font color="#800000">Setter.Value</font><font color="#0000ff">&gt;</font><br />
Â Â Â Â Â Â Â Â  <font color="#0000ff">&lt;/</font><font color="#800000">Setter</font><font color="#0000ff">&gt;<br />
Â Â  &lt;<font color="#800000">Sette</font><font color="#800000">r</font> <font color="#ff0000">Property</font><font color="#000000">=&#8221;</font>Template<font color="#000000">&#8220;</font>&gt;<br />
Â Â Â Â Â  &#8230;<br />
&lt;/<font color="#800000">Style</font>&gt;</font></p>
<p>Â and my column headers look like this in the compositionÂ (zoomed in to 200%):</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/4_zoomedincolumnpic.png" alt="4_ZoomedInColumnPic" /></p>
<p>But wait&#8230; there&#8217;s more. Right click on your ColumnHeaderContainerStyle in the Objects and Timeline panel and select:</p>
<p><strong>Edit Control Parts (Template) -&gt; Edit Templateâ€¦</strong></p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/5_edittemplate.png" alt="5_EditTemplate" /></p>
<p>Finally, we are in the real guts of the header template, which means we have all the bits and pieces to make it work.</p>
<p>Note: I&#8217;ve recently had a comment on how to style the header gripper (the part of the column header that allows you to resize the column), but this post is already getting way too long, so I deal with it in this post over here (coming soon).</p>
<p>OK, so now we can really get at the look of this sucker. If you&#8217;re using Vista, you probably have some Borders with names like &#8220;UpperHighlight&#8221;, &#8220;HeaderHoverBorder&#8221;, and &#8220;HeaderPressBorder&#8221;. These are borders that alter the look of the headers when certain triggers are active. We&#8217;ll be ignoring them for the sake of simplicity.</p>
<p>(At this point, my readers are saying to themselves, &#8220;Simplicity? SIMPLICITY? At what juncture in this entire post have we even approached simplicity.&#8221; To which I reply &#8220;Hey, don&#8217;t kill the messenger.&#8221;)</p>
<p>So go to the highest border (in my case, it&#8217;s called the &#8220;HeaderBorder&#8221;) and do the following:</p>
<ul>
<li><font color="#ff0000">BorderThickness</font>=&#8221;<font color="#0000ff">1,1,1,1</font>&#8220;</li>
<li><font color="#ff0000">CornerRadius</font>= &#8220;<font color="#0000ff">6,6,6,6</font>&#8220;</li>
<li><font color="#ff0000">Margin</font>=&#8221;<font color="#0000ff">2,0,2,0</font>&#8220;</li>
</ul>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/6_borderthicknesscorner.png" alt="6_BorderThicknessCorner" /><br />
<img src="http://www.designerwpf.com/wp-content/uploads/2008/01/7_margins.png" alt="7_Margins" /></p>
<p>The header now looks like this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/8_headerview2.png" alt="8_HeaderView2" /></p>
<p>This is the hard way because it uses Blend exclusively without touching the XAML.</p>
<h2>The Easy Way</h2>
<p>The easy way is also the better way from aÂ XAML perspective.Â Simply create a Â create a Style like so:</p>
<p>Â <font color="#0000ff">&lt;</font><font color="#800000">Style</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">CustomHeaderStyle</font>&#8221; <font color="#ff0000">TargetType</font>=&#8221;<font color="#cb860b">{x:Type GridViewColumnHeader}</font>&#8220;<font color="#0000ff">&gt;</font><br />
&lt;/Style&gt;</p>
<p>and add it to your ListView like this:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">ListView</font><font color="#0000ff">&gt;</font><br />
Â Â Â Â Â  <font color="#0000ff">&lt;</font><font color="#800000">ListView.View</font><font color="#0000ff">&gt;<br />
</font>Â Â Â Â Â Â Â Â Â Â Â  <font color="#0000ff">&lt;</font><font color="#800000">GridView</font> <font color="#ff0000">ColumnHeaderContainerStyle</font>=&#8221;<font color="#cb860b">{DynamicResource CustomHeaderStyle}</font>&#8220;<font color="#0000ff">&gt;</font><br />
Â Â Â Â Â Â Â Â Â Â Â  &#8230;.</p>
<p>Access it though your resources tab to get to the visual designer interface for your new style:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/11_resourcesscreen.png" alt="11_resourcesScreen" /></p>
<p>Once you&#8217;ve done this, you can skip directly to the part of the tutorial where you start changing the foreground and background.</p>
<p>Hopefully, at this point you feel like you have a pretty decent handle on the basics of styling your header section. Because it&#8217;s getting really late and I&#8217;m getting really tired.</p>
<p>But before I go, let&#8217;s get one thing absolutely clear: When it comes to ListView headers, you can technically do most of this in the data template, but if you do, you&#8217;ll make baby Jesus cry.</p>
<p>Here is why. By doing things as we have done them above, we get the following:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/9_headerview3.png" alt="9_HeaderView3" /></p>
<p>If we do all this work in the data template, we instead get this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/01/10_headerview4.png" alt="10_HeaderView4" /></p>
<p>Which brings about the aforementioned consequences. You&#8217;ve been warned.</p>
<p>Questions about column headers:</p>
<ul>
<li><a href="http://www.designerwpf.com/2008/02/02/how-do-i-wrap-text-in-a-listview-header/">How do I wrap text in a header?</a></li>
<li>How do I make headers sort columns (and how do I style for that)? (coming soon)</li>
<li>How do I make one header look different than the other headers? (coming soon)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/styling-the-listview-column-header/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
