<?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 can I get a ListBoxItem from a data bound ListBox?</title>
	<atom:link href="http://bea.stollnitz.com/blog/?feed=rss2&#038;p=7" rel="self" type="application/rss+xml" />
	<link>http://bea.stollnitz.com/blog/?p=7</link>
	<description>on Silverlight and WPF</description>
	<lastBuildDate>Thu, 22 Jul 2010 20:56:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Mike Pelton</title>
		<link>http://bea.stollnitz.com/blog/?p=7&#038;cpage=1#comment-190871</link>
		<dc:creator>Mike Pelton</dc:creator>
		<pubDate>Fri, 26 Feb 2010 16:40:15 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=7#comment-190871</guid>
		<description>Just as useful in 2010 as it was in 2005. Many thanks!</description>
		<content:encoded><![CDATA[<p>Just as useful in 2010 as it was in 2005. Many thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=7&#038;cpage=1#comment-185089</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Thu, 21 Jan 2010 18:25:53 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=7#comment-185089</guid>
		<description>Hi Paul,

Relying too much on the visuals is in general not a good practice. For example, if you decide to have a designer style your app which contains a ListBox, it could be that the a certain visual element you had initially is removed. If you&#039;re relying on it for the logic of  your program, the app will break. In WPF, this can also be an issue when the user changes the windows theme. If you don&#039;t override the default styles, controls will have different visual trees in different themes, and it&#039;s not a good idea to rely of visual elements of these styles. This really comes down to striving to achieve the best possible separation between UI and logic.

Having said that, there are scenarios where you can&#039;t avoid having to walk the visual tree. For those scenarios, I use the &quot;FindDescendents&quot; method that I paste in my reply to Oli, further down in the comments to this blog post. This method works in Silverlight without changes.

Let me know if this helps.

Bea</description>
		<content:encoded><![CDATA[<p>Hi Paul,</p>
<p>Relying too much on the visuals is in general not a good practice. For example, if you decide to have a designer style your app which contains a ListBox, it could be that the a certain visual element you had initially is removed. If you&#8217;re relying on it for the logic of  your program, the app will break. In WPF, this can also be an issue when the user changes the windows theme. If you don&#8217;t override the default styles, controls will have different visual trees in different themes, and it&#8217;s not a good idea to rely of visual elements of these styles. This really comes down to striving to achieve the best possible separation between UI and logic.</p>
<p>Having said that, there are scenarios where you can&#8217;t avoid having to walk the visual tree. For those scenarios, I use the &#8220;FindDescendents&#8221; method that I paste in my reply to Oli, further down in the comments to this blog post. This method works in Silverlight without changes.</p>
<p>Let me know if this helps.</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul</title>
		<link>http://bea.stollnitz.com/blog/?p=7&#038;cpage=1#comment-183773</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Thu, 14 Jan 2010 05:58:33 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=7#comment-183773</guid>
		<description>Hello Bea,

Could you link me to some more explanation as to why this is discouraged?

I need to do this in SILVERLIGHT and not WPF. To the previous commenter and those that will read this in future, note that the MSDN has a great example of accessing a textbox inside a WPF datatemplate here: http://msdn.microsoft.com/en-us/library/bb613579.aspx

Now, any clues on silverlight? I&#039;m stuck without this nifty &quot;FindVisualChild&quot; bit when I&#039;m using Silverlight! Can&#039;t get the VisualTreeHelper to do anything either.</description>
		<content:encoded><![CDATA[<p>Hello Bea,</p>
<p>Could you link me to some more explanation as to why this is discouraged?</p>
<p>I need to do this in SILVERLIGHT and not WPF. To the previous commenter and those that will read this in future, note that the MSDN has a great example of accessing a textbox inside a WPF datatemplate here: <a href="http://msdn.microsoft.com/en-us/library/bb613579.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/bb613579.aspx</a></p>
<p>Now, any clues on silverlight? I&#8217;m stuck without this nifty &#8220;FindVisualChild&#8221; bit when I&#8217;m using Silverlight! Can&#8217;t get the VisualTreeHelper to do anything either.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=7&#038;cpage=1#comment-170249</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Fri, 20 Nov 2009 19:12:42 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=7#comment-170249</guid>
		<description>Hi Andy,

Right - if the ListBoxItem has been virtualized, you can&#039;t get to it using this method. You can turn off virtualization by using a StackPanel as the ListBox panel, as you mention. That will work well if you&#039;re only displaying a few items, but if you have more than several hundred items, you will see that the performance degrades significantly.

One option you may want to consider is to use UIAutomation to find the items you want and virtualize them if necessary. WPF 4.0 introduces two new automation patterns that allow you to do that - ItemContainerPattern and VirtualizedItemPattern. ItemContainerPattern allows you to find a container regardless of whether it has been virtualized, and VirtualizedItemPattern allows you to &quot;de-virtualize&quot; (or &quot;realize&quot;) a specific virtualized item. This is very useful in testing, and may be useful for your scenario too. 

Bea</description>
		<content:encoded><![CDATA[<p>Hi Andy,</p>
<p>Right &#8211; if the ListBoxItem has been virtualized, you can&#8217;t get to it using this method. You can turn off virtualization by using a StackPanel as the ListBox panel, as you mention. That will work well if you&#8217;re only displaying a few items, but if you have more than several hundred items, you will see that the performance degrades significantly.</p>
<p>One option you may want to consider is to use UIAutomation to find the items you want and virtualize them if necessary. WPF 4.0 introduces two new automation patterns that allow you to do that &#8211; ItemContainerPattern and VirtualizedItemPattern. ItemContainerPattern allows you to find a container regardless of whether it has been virtualized, and VirtualizedItemPattern allows you to &#8220;de-virtualize&#8221; (or &#8220;realize&#8221;) a specific virtualized item. This is very useful in testing, and may be useful for your scenario too. </p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=7&#038;cpage=1#comment-170154</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Fri, 20 Nov 2009 02:56:03 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=7#comment-170154</guid>
		<description>Hi Oli,

I try as much as I can not to rely on visual tree walks because it&#039;s brittle. Next time someone changes the DataTemplate, your code that relied on a particular element being there may fail. Having said that, there are certainly scenarios where walking the visual tree can be very useful. I use the following method to find descendents of a particular element:

        public static T FindDescendent&lt;T&gt;(DependencyObject element) where T : class
        {
            Stack&lt;DependencyObject&gt; stack = new Stack&lt;DependencyObject&gt;();

            for (int i = 0; i &lt; VisualTreeHelper.GetChildrenCount(element); i++)
            {
                stack.Push(VisualTreeHelper.GetChild(element, i));
            }

            while (stack.Count &gt; 0)
            {
                DependencyObject poppedElement = stack.Pop();
                T tPoppedElement = poppedElement as T;
                if (tPoppedElement != null)
                {
                    return tPoppedElement;
                }
                for (int i = 0; i &lt; VisualTreeHelper.GetChildrenCount(poppedElement); i++)
                {
                    stack.Push(VisualTreeHelper.GetChild(poppedElement, i));
                }
            }

            return null;
        }

Hope this helps.

Bea</description>
		<content:encoded><![CDATA[<p>Hi Oli,</p>
<p>I try as much as I can not to rely on visual tree walks because it&#8217;s brittle. Next time someone changes the DataTemplate, your code that relied on a particular element being there may fail. Having said that, there are certainly scenarios where walking the visual tree can be very useful. I use the following method to find descendents of a particular element:</p>
<p>        public static T FindDescendent<t>(DependencyObject element) where T : class<br />
        {<br />
            Stack<dependencyobject> stack = new Stack</dependencyobject><dependencyobject>();</p>
<p>            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)<br />
            {<br />
                stack.Push(VisualTreeHelper.GetChild(element, i));<br />
            }</p>
<p>            while (stack.Count > 0)<br />
            {<br />
                DependencyObject poppedElement = stack.Pop();<br />
                T tPoppedElement = poppedElement as T;<br />
                if (tPoppedElement != null)<br />
                {<br />
                    return tPoppedElement;<br />
                }<br />
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(poppedElement); i++)<br />
                {<br />
                    stack.Push(VisualTreeHelper.GetChild(poppedElement, i));<br />
                }<br />
            }</p>
<p>            return null;<br />
        }</p>
<p>Hope this helps.</p>
<p>Bea</dependencyobject></t></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=7&#038;cpage=1#comment-166465</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Sun, 11 Oct 2009 20:26:52 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=7#comment-166465</guid>
		<description>Hi jason,

You can add MouseEnter and MouseLeave events to the ListBoxItem&#039;s Style, with the help of an event setter. In general styles and templates allow you to make significant changes to the ListBoxItems at the time they&#039;re created.

Bea</description>
		<content:encoded><![CDATA[<p>Hi jason,</p>
<p>You can add MouseEnter and MouseLeave events to the ListBoxItem&#8217;s Style, with the help of an event setter. In general styles and templates allow you to make significant changes to the ListBoxItems at the time they&#8217;re created.</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=7&#038;cpage=1#comment-166464</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Sun, 11 Oct 2009 20:22:44 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=7#comment-166464</guid>
		<description>Hi Turner,

Yes, as you guessed that&#039;s because the ListBoxItems are virtualized. The only way around it is to turn off virtualization, which you can do by telling the ListBox to use a StackPanel instea of a VirtualizingStackPanel to host its items. Here&#039;s the syntax:

    &lt;ListBox ItemsSource=&quot;{Binding}&quot;&gt;
        &lt;ListBox.ItemsPanel&gt;
            &lt;ItemsPanelTemplate&gt;
                &lt;StackPanel /&gt;
            &lt;/ItemsPanelTemplate&gt;
        &lt;/ListBox.ItemsPanel&gt;
    &lt;/ListBox&gt;

You can read more about virtualization in the following post: &lt;a href=&quot;http://bea.stollnitz.com/blog/?p=338&quot; rel=&quot;nofollow&quot;&gt;http://bea.stollnitz.com/blog/?p=338&lt;/a&gt;.

Bea</description>
		<content:encoded><![CDATA[<p>Hi Turner,</p>
<p>Yes, as you guessed that&#8217;s because the ListBoxItems are virtualized. The only way around it is to turn off virtualization, which you can do by telling the ListBox to use a StackPanel instea of a VirtualizingStackPanel to host its items. Here&#8217;s the syntax:</p>
<p>    &lt;ListBox ItemsSource=&#8221;{Binding}&#8221;&gt;<br />
        &lt;ListBox.ItemsPanel&gt;<br />
            &lt;ItemsPanelTemplate&gt;<br />
                &lt;StackPanel /&gt;<br />
            &lt;/ItemsPanelTemplate&gt;<br />
        &lt;/ListBox.ItemsPanel&gt;<br />
    &lt;/ListBox&gt;</p>
<p>You can read more about virtualization in the following post: <a href="http://bea.stollnitz.com/blog/?p=338" rel="nofollow">http://bea.stollnitz.com/blog/?p=338</a>.</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=7&#038;cpage=1#comment-166452</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Sun, 11 Oct 2009 18:19:31 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=7#comment-166452</guid>
		<description>Yes, it should, thanks for letting me know. I&#039;ve corrected that in the post.

Bea</description>
		<content:encoded><![CDATA[<p>Yes, it should, thanks for letting me know. I&#8217;ve corrected that in the post.</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BlueSky</title>
		<link>http://bea.stollnitz.com/blog/?p=7&#038;cpage=1#comment-166289</link>
		<dc:creator>BlueSky</dc:creator>
		<pubDate>Fri, 09 Oct 2009 15:39:18 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=7#comment-166289</guid>
		<description>Typo?

ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(listBox.Items.CurrentItem));

should be:

ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items.CurrentItem));</description>
		<content:encoded><![CDATA[<p>Typo?</p>
<p>ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(listBox.Items.CurrentItem));</p>
<p>should be:</p>
<p>ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items.CurrentItem));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oli</title>
		<link>http://bea.stollnitz.com/blog/?p=7&#038;cpage=1#comment-163199</link>
		<dc:creator>Oli</dc:creator>
		<pubDate>Fri, 11 Sep 2009 10:41:54 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=7#comment-163199</guid>
		<description>Hi,
Learning WPF I&#039;ve come across a number of great articles from you, thanks!

I have a question similar to the one asked about getting reference to a particular control within the listboxitem. I need to do the same thing here as I am looking to change the itemsSource on a particualr comboBox contained within my listboxes datatemplate.

Would it be possible for you to ellaborate on this please - I haven&#039;t been able to find a good example of using VisualOperations.GetChildren. 

One last thing, you mention that this is something to be discouraged. Why is that?

Thanks very much.</description>
		<content:encoded><![CDATA[<p>Hi,<br />
Learning WPF I&#8217;ve come across a number of great articles from you, thanks!</p>
<p>I have a question similar to the one asked about getting reference to a particular control within the listboxitem. I need to do the same thing here as I am looking to change the itemsSource on a particualr comboBox contained within my listboxes datatemplate.</p>
<p>Would it be possible for you to ellaborate on this please &#8211; I haven&#8217;t been able to find a good example of using VisualOperations.GetChildren. </p>
<p>One last thing, you mention that this is something to be discouraged. Why is that?</p>
<p>Thanks very much.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
