<?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>Ben Dewey - Web, XAML, and Cloud &#187; Controls</title>
	<atom:link href="http://www.bendewey.com/index.php/tag/controls/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bendewey.com</link>
	<description>Father, Developer, Microsoft MVP, Speaker, and Author</description>
	<lastBuildDate>Sat, 22 Mar 2025 05:04:02 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.8.41</generator>
	<item>
		<title>Alternating Row Color in Windows Store ListView</title>
		<link>http://www.bendewey.com/index.php/523/alternating-row-color-in-windows-store-listview</link>
		<comments>http://www.bendewey.com/index.php/523/alternating-row-color-in-windows-store-listview#comments</comments>
		<pubDate>Wed, 22 Oct 2014 15:08:50 +0000</pubDate>
		<dc:creator><![CDATA[bendewey]]></dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Controls]]></category>
		<category><![CDATA[Windows8]]></category>

		<guid isPermaLink="false">http://www.bendewey.com/?p=523</guid>
		<description><![CDATA[I&#8217;ve always been drawn to XAML for its powerful ability to customize design to any extent. On a recent Windows Store project I found it.]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve always been drawn to XAML for its powerful ability to customize design to any extent.  On a recent Windows Store project I found it difficult to create the alternating row color, or zebra striping, effect on a ListView control, something that&#8217;s proven to be easy with CSS3.  I decided to write this post in hopes that others would find this technique useful.</p>
<p>I search online and found a technique that included adding an index property to the model in question and then adding a converter to the DataTemplate.  This wasn&#8217;t ideal because it only changed the contents of the list item, so depending on padding and content alignment you&#8217;d see gaps around the row background.  I also didn&#8217;t like the code smell of modifying my data model objects with UI code.</p>
<p>What I wanted was to override the ListViewItem control&#8217;s Background property.  You could use the ListView.ItemContainerStyle property and set the background using a style, but unfortunately the style&#8217;s context wouldn&#8217;t have any way of knowing the index of the item relative to the full source collection.</p>
<p>The option I used was to override a ListView control and create an AlternatingRowListView.  Then by overriding the PrepareContainerForItemOverride, which is responsible for the setting up the ListViewItem control after it&#8217;s be created, you could modify the background color.</p>
<pre class="brush: csharp; title: ; notranslate">
public class AlternatingRowListView : ListView
{

    protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
    {
        base.PrepareContainerForItemOverride(element, item);
        var listViewItem = element as ListViewItem;
        if (listViewItem != null)
        {
            var index = IndexFromContainer(element);

            if ((index + 1) % 2 == 1)
            {
                listViewItem.Background = new SolidColorBrush(Colors.White);
            }
            else
            {
                listViewItem.Background = new SolidColorBrush(Colors.Gray);
            }
        }
        
    }
}
</pre>
<p>This code uses a method on the ListView base class called ItemFromContainer(), which simply gives you the index of the item relative to the ListView&#8217;s source collection.  With that index you can set the ListViewItem&#8217;s Background color based on a modulus of 2.  Although, using hard coded colors for the background doesn&#8217;t make it very usable across other designs.  This is where dependency properties come into play.  They allow the control to expose properties to XAML, where you can set them to any Brush or Resource you&#8217;d like.  To do this you have to specify them as they are below and update your overridden method from before with these new properties.</p>
<pre class="brush: csharp; title: ; notranslate">
public class AlternatingRowListView : ListView
{
	public static readonly DependencyProperty OddRowBackgroundProperty =
		DependencyProperty.Register(&quot;OddRowBackground&quot;, typeof(Brush), typeof(AlternatingRowListView), null);
	public Brush OddRowBackground
	{
		get { return (Brush)GetValue(OddRowBackgroundProperty); }
		set { SetValue(OddRowBackgroundProperty, (Brush)value); }
	}
	
	public static readonly DependencyProperty EvenRowBackgroundProperty =
		DependencyProperty.Register(&quot;EvenRowBackground&quot;, typeof(Brush), typeof(AlternatingRowListView), null);
	public Brush EvenRowBackground
	{
		get { return (Brush)GetValue(EvenRowBackgroundProperty); }
		set { SetValue(EvenRowBackgroundProperty, (Brush)value); }
	}

    protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
    {
        base.PrepareContainerForItemOverride(element, item);
        var listViewItem = element as ListViewItem;
        if (listViewItem != null)
        {
            var index = IndexFromContainer(element);

            if ((index + 1) % 2 == 1)
            {
                listViewItem.Background = OddRowBackground;
            }
            else
            {
                listViewItem.Background = EvenRowBackground;
            }
        }
        
    }
}
</pre>
<p>After this, you can put it all together by using your new control in place of a ListView.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;Page
    x:Name=&quot;pageRoot&quot;
    x:Class=&quot;ZebraStripedListView.ItemsPage&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:controls=&quot;using:ZebraStripedListView.Controls&quot;&gt;

	&lt;controls:AlternatingRowListView 
			ItemsSource=&quot;{Binding Source={StaticResource itemsViewSource}}&quot;
			OddRowBackground=&quot;{ThemeResource GridOddRowBackgroundBrush}&quot; EvenRowBackground=&quot;{ThemeResource GridEvenRowBackgroundBrush}&quot;&gt;
		&lt;controls:AlternatingRowListView.ItemTemplate&gt;
			&lt;DataTemplate&gt;
				&lt;Grid&gt;
					&lt;Grid.ColumnDefinitions&gt;
						&lt;ColumnDefinition Width=&quot;300&quot; /&gt;
						&lt;ColumnDefinition Width=&quot;*&quot; /&gt;
					&lt;/Grid.ColumnDefinitions&gt;
					&lt;TextBlock Text=&quot;{Binding Title}&quot; Grid.Column=&quot;0&quot; /&gt;
					&lt;TextBlock Text=&quot;{Binding Subtitle}&quot; Grid.Column=&quot;1&quot; /&gt;
				&lt;/Grid&gt;
			&lt;/DataTemplate&gt;
		&lt;/controls:AlternatingRowListView.ItemTemplate&gt;
	&lt;/controls:AlternatingRowListView&gt;
&lt;/Page&gt;
</pre>
<p>This is what the final result looks like for my sample code.</p>
<a href="http://www.bendewey.com/wp-content/uploads/2014/10/screenshot_10222014_104159.png"><img src="http://www.bendewey.com/wp-content/uploads/2014/10/screenshot_10222014_104159-1024x575.png" alt="Zerbra Striped ListView" width="700" height="393" class="aligncenter size-large wp-image-524" /></a>
<p>I&#8217;ve uploaded the full code to my GitHub account at <a href="http://github.com/bendewey/ZebraStripedListView">http://github.com/bendewey/ZebraStripedListView</a>.  I hope this is useful to other who encounter a similar scenario.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bendewey.com/index.php/523/alternating-row-color-in-windows-store-listview/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Swoosh Button</title>
		<link>http://www.bendewey.com/index.php/4/swoosh-button</link>
		<comments>http://www.bendewey.com/index.php/4/swoosh-button#comments</comments>
		<pubDate>Thu, 14 Feb 2008 15:21:18 +0000</pubDate>
		<dc:creator><![CDATA[bendewey]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Controls]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://bendewey.wordpress.com/?p=4</guid>
		<description><![CDATA[I created this little swoosh button, as I call it, as my main button design for most of my apps. There are three colors for.]]></description>
				<content:encoded><![CDATA[<p>I created this little swoosh button, as I call it, as my main button design for most of my apps.  There are three colors for now but it can easily be adapted to support multiple more. There is a standard blue color, a red color (i used for the cancel buttons), and a green color (I used for the OK and Next Buttons) .  I also think the button looks really good with all caps text.</p>
<p>I keep trying to come up with a good way to pass the color in without creating a custom control.  If anyone has any thought please feel free to share.</p>
<p><a href="http://www.bendewey.name/code/swooshButton.html" title="Code" target="_blank"></a>Enjoy</p>
<img src="http://bendewey.files.wordpress.com/2008/02/button.jpg" alt="SwooshButton" /><img src="http://bendewey.files.wordpress.com/2008/02/buttonover.thumbnail.jpg" alt="BlueSwoosh" />
<h3>Usage</h3>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">&lt;</span><span style="font-size:10pt;font-family:'Courier New';color:#a31515;">Button</span><span style="font-size:10pt;font-family:'Courier New';color:red;"> Width</span><span style="font-size:10pt;font-family:'Courier New';color:blue;">=&#8221;60&#8243;</span><span style="font-size:10pt;font-family:'Courier New';color:red;"> x</span><span style="font-size:10pt;font-family:'Courier New';color:blue;">:</span><span style="font-size:10pt;font-family:'Courier New';color:red;">Name</span><span style="font-size:10pt;font-family:'Courier New';color:blue;">=&#8221;browse&#8221;</span><span style="font-size:10pt;font-family:'Courier New';color:red;"> Style</span><span style="font-size:10pt;font-family:'Courier New';color:blue;">=&#8221;{</span><span style="font-size:10pt;font-family:'Courier New';color:#a31515;">StaticResource</span><span style="font-size:10pt;font-family:'Courier New';color:red;"> SwooshButton</span><span style="font-size:10pt;font-family:'Courier New';color:blue;">}&#8221;&gt;</span><span style="font-size:10pt;font-family:'Courier New';color:#a31515;">Browse</span><span style="font-size:10pt;font-family:'Courier New';color:blue;">&lt;/</span><span style="font-size:10pt;font-family:'Courier New';color:#a31515;">Button</span><span style="font-size:10pt;font-family:'Courier New';color:blue;">&gt;</span></p>
<h3>Code</h3>
<p><a href="http://www.bendewey.name/code/swooshButton.html" title="Code" target="_blank">http://www.bendewey.name/code/swooshButton.html</a></p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bendewey.com/index.php/4/swoosh-button/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
