<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: How do I implement a data bound ListView?</title>
	<atom:link href="http://bea.stollnitz.com/blog/?feed=rss2&#038;p=20" rel="self" type="application/rss+xml" />
	<link>http://bea.stollnitz.com/blog/?p=20</link>
	<description>on Silverlight and WPF</description>
	<lastBuildDate>Mon, 02 Aug 2010 17:57:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=20&#038;cpage=1#comment-185131</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Thu, 21 Jan 2010 23:07:26 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=20#comment-185131</guid>
		<description>Hi Alan,

Thanks for sharing your code.

Also, keep in mind that WPF 3.5 has a built-in feature that allows alternating rows in a more efficient way than my solution here. You can read more about it in &lt;a href=&quot;http://blogs.msdn.com/vinsibal/archive/2008/05/28/wpf-3-5-sp1-feature-alternating-rows.aspx&quot; rel=&quot;nofollow&quot;&gt;Vincent&#039;s blog&lt;/a&gt;.

Thanks,
Bea</description>
		<content:encoded><![CDATA[<p>Hi Alan,</p>
<p>Thanks for sharing your code.</p>
<p>Also, keep in mind that WPF 3.5 has a built-in feature that allows alternating rows in a more efficient way than my solution here. You can read more about it in <a href="http://blogs.msdn.com/vinsibal/archive/2008/05/28/wpf-3-5-sp1-feature-alternating-rows.aspx" rel="nofollow">Vincent&#8217;s blog</a>.</p>
<p>Thanks,<br />
Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alan</title>
		<link>http://bea.stollnitz.com/blog/?p=20&#038;cpage=1#comment-173987</link>
		<dc:creator>Alan</dc:creator>
		<pubDate>Thu, 10 Dec 2009 00:20:14 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=20#comment-173987</guid>
		<description>I took your ListViewItemStyleSelector class and added a bit of functionality to it that I thought I would share.  I setup the class to take an arbitrary number of styles to alternate between (a minimum of 2).

public class ListViewItemAlternatingStyleSelector : StyleSelector
{
	private int i = 0;
	private List&lt;string&gt; styleKeys;

	public ListViewItemAlternatingStyleSelector(string styleKey1, string styleKey2, params string[] styleKeys)
	{
		this.styleKeys = new List&lt;string&gt;(styleKeys.Length + 2);
		this.styleKeys.Add(styleKey1);
		this.styleKeys.Add(styleKey2);
		this.styleKeys.AddRange(styleKeys);
	}

	public override Style SelectStyle(object item, DependencyObject container)
	{
		ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(container);

		if (item == ic.Items[0])
		{
			i = 0;
		}

		return (Style)ic.FindResource(styleKeys[i++ % this.styleKeys.Count]);
	}
}

To construct the class in XAML, you simply use an ObjectDataProvider.

&lt;ObjectDataProvider x:Key=&quot;ListViewItemAlternatingStyleSelector&quot; ObjectType=&quot;{x:Type ctrls:ListViewItemAlternatingStyleSelector}&quot;&gt;
	&lt;ObjectDataProvider.ConstructorParameters&gt;
		&lt;sys:String&gt;ListViewItemStyle1&lt;/sys:String&gt;
		&lt;sys:String&gt;ListViewItemStyle2&lt;/sys:String&gt;
		&lt;sys:String&gt;ListViewItemStyle3&lt;/sys:String&gt;
		&lt;sys:String&gt;ListViewItemStyle4&lt;/sys:String&gt;
	&lt;/ObjectDataProvider.ConstructorParameters&gt;
&lt;/ObjectDataProvider&gt;            
&lt;Style TargetType=&quot;{x:Type ListViewItem}&quot; x:Key=&quot;ListViewItemStyle1&quot;&gt;
	&lt;Setter Property=&quot;Background&quot; Value=&quot;Transparent&quot; /&gt;
&lt;/Style&gt;
&lt;Style TargetType=&quot;{x:Type ListViewItem}&quot; x:Key=&quot;ListViewItemStyle2&quot;&gt;
	&lt;Setter Property=&quot;Background&quot; Value=&quot;Honeydew&quot; /&gt;
&lt;/Style&gt;
&lt;Style TargetType=&quot;{x:Type ListViewItem}&quot; x:Key=&quot;ListViewItemStyle3&quot;&gt;
	&lt;Setter Property=&quot;Background&quot; Value=&quot;Transparent&quot; /&gt;
&lt;/Style&gt;
&lt;Style TargetType=&quot;{x:Type ListViewItem}&quot; x:Key=&quot;ListViewItemStyle4&quot;&gt;
	&lt;Setter Property=&quot;Background&quot; Value=&quot;AliceBlue&quot; /&gt;
&lt;/Style&gt;

And of course we must update the binding to support the ObjectDataProvider.

&lt;ListView ItemContainerStyleSelector=&quot;{Binding Source={StaticResource ListViewItemAlternatingStyleSelector}}&quot;&gt;</description>
		<content:encoded><![CDATA[<p>I took your ListViewItemStyleSelector class and added a bit of functionality to it that I thought I would share.  I setup the class to take an arbitrary number of styles to alternate between (a minimum of 2).</p>
<p>public class ListViewItemAlternatingStyleSelector : StyleSelector<br />
{<br />
	private int i = 0;<br />
	private List&lt;string&gt; styleKeys;</p>
<p>	public ListViewItemAlternatingStyleSelector(string styleKey1, string styleKey2, params string[] styleKeys)<br />
	{<br />
		this.styleKeys = new List&lt;string&gt;(styleKeys.Length + 2);<br />
		this.styleKeys.Add(styleKey1);<br />
		this.styleKeys.Add(styleKey2);<br />
		this.styleKeys.AddRange(styleKeys);<br />
	}</p>
<p>	public override Style SelectStyle(object item, DependencyObject container)<br />
	{<br />
		ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(container);</p>
<p>		if (item == ic.Items[0])<br />
		{<br />
			i = 0;<br />
		}</p>
<p>		return (Style)ic.FindResource(styleKeys[i++ % this.styleKeys.Count]);<br />
	}<br />
}</p>
<p>To construct the class in XAML, you simply use an ObjectDataProvider.</p>
<p>&lt;ObjectDataProvider x:Key=&#8221;ListViewItemAlternatingStyleSelector&#8221; ObjectType=&#8221;{x:Type ctrls:ListViewItemAlternatingStyleSelector}&#8221;&gt;<br />
	&lt;ObjectDataProvider.ConstructorParameters&gt;<br />
		&lt;sys:String&gt;ListViewItemStyle1&lt;/sys:String&gt;<br />
		&lt;sys:String&gt;ListViewItemStyle2&lt;/sys:String&gt;<br />
		&lt;sys:String&gt;ListViewItemStyle3&lt;/sys:String&gt;<br />
		&lt;sys:String&gt;ListViewItemStyle4&lt;/sys:String&gt;<br />
	&lt;/ObjectDataProvider.ConstructorParameters&gt;<br />
&lt;/ObjectDataProvider&gt;<br />
&lt;Style TargetType=&#8221;{x:Type ListViewItem}&#8221; x:Key=&#8221;ListViewItemStyle1&#8243;&gt;<br />
	&lt;Setter Property=&#8221;Background&#8221; Value=&#8221;Transparent&#8221; /&gt;<br />
&lt;/Style&gt;<br />
&lt;Style TargetType=&#8221;{x:Type ListViewItem}&#8221; x:Key=&#8221;ListViewItemStyle2&#8243;&gt;<br />
	&lt;Setter Property=&#8221;Background&#8221; Value=&#8221;Honeydew&#8221; /&gt;<br />
&lt;/Style&gt;<br />
&lt;Style TargetType=&#8221;{x:Type ListViewItem}&#8221; x:Key=&#8221;ListViewItemStyle3&#8243;&gt;<br />
	&lt;Setter Property=&#8221;Background&#8221; Value=&#8221;Transparent&#8221; /&gt;<br />
&lt;/Style&gt;<br />
&lt;Style TargetType=&#8221;{x:Type ListViewItem}&#8221; x:Key=&#8221;ListViewItemStyle4&#8243;&gt;<br />
	&lt;Setter Property=&#8221;Background&#8221; Value=&#8221;AliceBlue&#8221; /&gt;<br />
&lt;/Style&gt;</p>
<p>And of course we must update the binding to support the ObjectDataProvider.</p>
<p>&lt;ListView ItemContainerStyleSelector=&#8221;{Binding Source={StaticResource ListViewItemAlternatingStyleSelector}}&#8221;&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=20&#038;cpage=1#comment-157399</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Sun, 26 Jul 2009 21:58:00 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=20#comment-157399</guid>
		<description>Hi Mark,

That would simplify the code, but it would make it less performant. IndexOf does a linear search - O(n) - so doing that for each item would result in a O(n2) solution. My solution is a bit more verbose, but it executes in O(n).

Also, WPF now has support for alternating rows. To get this working, you can set ItemsControl&#039;s AlternationCount property to the number of different styles you want to have. Then you can use a trigger to set different properties for each row. For more information, look at &lt;a href=&quot;http://blogs.msdn.com/vinsibal/archive/2008/05/28/wpf-3-5-sp1-feature-alternating-rows.aspx&quot; rel=&quot;nofollow&quot;&gt;Vincent&#039;s blog post&lt;/a&gt;.

Bea</description>
		<content:encoded><![CDATA[<p>Hi Mark,</p>
<p>That would simplify the code, but it would make it less performant. IndexOf does a linear search &#8211; O(n) &#8211; so doing that for each item would result in a O(n2) solution. My solution is a bit more verbose, but it executes in O(n).</p>
<p>Also, WPF now has support for alternating rows. To get this working, you can set ItemsControl&#8217;s AlternationCount property to the number of different styles you want to have. Then you can use a trigger to set different properties for each row. For more information, look at <a href="http://blogs.msdn.com/vinsibal/archive/2008/05/28/wpf-3-5-sp1-feature-alternating-rows.aspx" rel="nofollow">Vincent&#8217;s blog post</a>.</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark</title>
		<link>http://bea.stollnitz.com/blog/?p=20&#038;cpage=1#comment-154862</link>
		<dc:creator>Mark</dc:creator>
		<pubDate>Tue, 14 Jul 2009 09:34:32 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=20#comment-154862</guid>
		<description>Hi Bea,

All your blogs are great. Just one thing you can use the indexOf property of the items collection to simplify your SelectStyle function:

		public override Style SelectStyle(object item, DependencyObject container)
		{
			var ic = ItemsControl.ItemsControlFromItemContainer(container);

			return (Style)(ic.FindResource(
                ic.Items.IndexOf(item) % 2 == 0 ? &quot;ListViewItemStyle1&quot; : &quot;ListViewItemStyle2&quot;));
		}</description>
		<content:encoded><![CDATA[<p>Hi Bea,</p>
<p>All your blogs are great. Just one thing you can use the indexOf property of the items collection to simplify your SelectStyle function:</p>
<p>		public override Style SelectStyle(object item, DependencyObject container)<br />
		{<br />
			var ic = ItemsControl.ItemsControlFromItemContainer(container);</p>
<p>			return (Style)(ic.FindResource(<br />
                ic.Items.IndexOf(item) % 2 == 0 ? &#8220;ListViewItemStyle1&#8243; : &#8220;ListViewItemStyle2&#8243;));<br />
		}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: abcd</title>
		<link>http://bea.stollnitz.com/blog/?p=20&#038;cpage=1#comment-154177</link>
		<dc:creator>abcd</dc:creator>
		<pubDate>Fri, 10 Jul 2009 07:45:38 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=20#comment-154177</guid>
		<description>Thx!</description>
		<content:encoded><![CDATA[<p>Thx!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=20&#038;cpage=1#comment-457</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Tue, 25 Sep 2007 17:20:22 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=20#comment-457</guid>
		<description>Hi Bill,

What was happening is that the text was wrapping, and therefore occupying a smaller width, but the total width of the ListView was not changing. My guess is that HorizontalAlignment was Center, causing the whole text to shift as we make its width smaller (as a consequence of resizing). By setting HorizontalAlignment to Left, the text will have no need to shift as it&#039;s resized anymore.

Does that make sense?

I&#039;m glad it worked for you!

Bea</description>
		<content:encoded><![CDATA[<p>Hi Bill,</p>
<p>What was happening is that the text was wrapping, and therefore occupying a smaller width, but the total width of the ListView was not changing. My guess is that HorizontalAlignment was Center, causing the whole text to shift as we make its width smaller (as a consequence of resizing). By setting HorizontalAlignment to Left, the text will have no need to shift as it&#8217;s resized anymore.</p>
<p>Does that make sense?</p>
<p>I&#8217;m glad it worked for you!</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill</title>
		<link>http://bea.stollnitz.com/blog/?p=20&#038;cpage=1#comment-452</link>
		<dc:creator>Bill</dc:creator>
		<pubDate>Tue, 25 Sep 2007 15:57:20 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=20#comment-452</guid>
		<description>Works beautifully.  I&#039;ve actually got a more complex scenario, with numerous controls below the GridViewRowPresenter, and I&#039;ve encased them all in a StackPanel with the HorizontalAlignment=&quot;Left&quot;, which definately fixes this odd behavior.  Now what I&#039;d love to understand is why we get the odd behavior and why this fixes it.  Knowing a few hacks like this is helpful, but understanding why they work allows you to apply similar hacks in other situations.  Any idea what&#039;s going on here?

In any event, thanks a million for helping me work around this specific scenario!</description>
		<content:encoded><![CDATA[<p>Works beautifully.  I&#8217;ve actually got a more complex scenario, with numerous controls below the GridViewRowPresenter, and I&#8217;ve encased them all in a StackPanel with the HorizontalAlignment=&#8221;Left&#8221;, which definately fixes this odd behavior.  Now what I&#8217;d love to understand is why we get the odd behavior and why this fixes it.  Knowing a few hacks like this is helpful, but understanding why they work allows you to apply similar hacks in other situations.  Any idea what&#8217;s going on here?</p>
<p>In any event, thanks a million for helping me work around this specific scenario!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=20&#038;cpage=1#comment-440</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Mon, 24 Sep 2007 21:46:30 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=20#comment-440</guid>
		<description>Hi Bill,

Yeah, I see the same behavior you&#039;re describing. I was able to get around it by setting the HorizontalAlignment property of the TextBlock to Left. Try this:

&lt;TextBlock Text=&quot;{Binding XPath=Details}&quot; HorizontalAlignment=&quot;Left&quot; MaxWidth=&quot;{Binding RelativeSource={RelativeSource AncestorType={x:Type ScrollViewer}}, Path=ViewportWidth}&quot; Visibility=&quot;Collapsed&quot; Name=&quot;tb&quot; TextWrapping=&quot;Wrap&quot; Margin=&quot;2,0,0,4&quot;/&gt;

Or you can download my sample &lt;a href=&quot;http://www.beacosta.com/BlogComments/36TextWrapListView.zip&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;.

Let me know if this worked for you.

Bea</description>
		<content:encoded><![CDATA[<p>Hi Bill,</p>
<p>Yeah, I see the same behavior you&#8217;re describing. I was able to get around it by setting the HorizontalAlignment property of the TextBlock to Left. Try this:</p>
<p>&lt;TextBlock Text=&#8221;{Binding XPath=Details}&#8221; HorizontalAlignment=&#8221;Left&#8221; MaxWidth=&#8221;{Binding RelativeSource={RelativeSource AncestorType={x:Type ScrollViewer}}, Path=ViewportWidth}&#8221; Visibility=&#8221;Collapsed&#8221; Name=&#8221;tb&#8221; TextWrapping=&#8221;Wrap&#8221; Margin=&#8221;2,0,0,4&#8243;/&gt;</p>
<p>Or you can download my sample <a href="http://www.beacosta.com/BlogComments/36TextWrapListView.zip" rel="nofollow">here</a>.</p>
<p>Let me know if this worked for you.</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill</title>
		<link>http://bea.stollnitz.com/blog/?p=20&#038;cpage=1#comment-436</link>
		<dc:creator>Bill</dc:creator>
		<pubDate>Mon, 24 Sep 2007 20:40:17 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=20#comment-436</guid>
		<description>The only problem with this solution is that now the view can be smaller than the width of the columns, and there&#039;s no way to scroll to see the data within the columns. :(

I&#039;ve tried a solution where you bind the MaxWidth to the ScrollViewer&#039;s ViewportWidth, which appears to work splendidly at first.  The problem is, if you resize the window decreasing the width, the text moves a corresponding size to the right as if you&#039;d set a left margin.  As of yet, I&#039;ve not found an ideal solution for this scenario. :(</description>
		<content:encoded><![CDATA[<p>The only problem with this solution is that now the view can be smaller than the width of the columns, and there&#8217;s no way to scroll to see the data within the columns. :(</p>
<p>I&#8217;ve tried a solution where you bind the MaxWidth to the ScrollViewer&#8217;s ViewportWidth, which appears to work splendidly at first.  The problem is, if you resize the window decreasing the width, the text moves a corresponding size to the right as if you&#8217;d set a left margin.  As of yet, I&#8217;ve not found an ideal solution for this scenario. :(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=20&#038;cpage=1#comment-354</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Tue, 18 Sep 2007 06:00:20 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=20#comment-354</guid>
		<description>Hi Bill,

Try setting ScrollViewer.HorizontalScrollBarVisibility=&quot;Disabled&quot; on your ListView. This worked for me, in the sample for this blog post.

ScrollViewer.HorizontalScrollBarVisibility is set to Auto by default, which means that a horizontal scrollbar comes up when the content of the rows can not all be displayed. Setting it to Disabled will force all the content to fit in the ListView area. If your text is too long, it will wrap (provided that you enabled text wrapping).

I uploaded a project to my server that shows this working - you can find it &lt;a href=&quot;http://www.beacosta.com/BlogComments/30TextWrapListView.zip&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;.

Thanks,
Bea</description>
		<content:encoded><![CDATA[<p>Hi Bill,</p>
<p>Try setting ScrollViewer.HorizontalScrollBarVisibility=&#8221;Disabled&#8221; on your ListView. This worked for me, in the sample for this blog post.</p>
<p>ScrollViewer.HorizontalScrollBarVisibility is set to Auto by default, which means that a horizontal scrollbar comes up when the content of the rows can not all be displayed. Setting it to Disabled will force all the content to fit in the ListView area. If your text is too long, it will wrap (provided that you enabled text wrapping).</p>
<p>I uploaded a project to my server that shows this working &#8211; you can find it <a href="http://www.beacosta.com/BlogComments/30TextWrapListView.zip" rel="nofollow">here</a>.</p>
<p>Thanks,<br />
Bea</p>
]]></content:encoded>
	</item>
</channel>
</rss>
