<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Andrew Arnott : WPF</title><link>http://blogs.msdn.com/andrewarnottms/archive/tags/WPF/default.aspx</link><description>Tags: WPF</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Visual Studio 2010 editor vs. gVim over Remote Desktop</title><link>http://blogs.msdn.com/andrewarnottms/archive/2009/10/19/visual-studio-2010-editor-vs-gvim-over-remote-desktop.aspx</link><pubDate>Mon, 19 Oct 2009 08:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9897053</guid><dc:creator>andarno</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/andrewarnottms/comments/9897053.aspx</comments><wfw:commentRss>http://blogs.msdn.com/andrewarnottms/commentrss.aspx?PostID=9897053</wfw:commentRss><description>&lt;p&gt;For those of you who may think WPF is too slow, you gotta check this out. Visual Studio 2010's new WPF-based code editor is &lt;i&gt;lightening&lt;/i&gt; fast, especially when compared over a fast FiOS Remote Desktop connection to gVim!&lt;/p&gt; &lt;iframe style="width: 500px; height: 375px" src="http://silverlight.services.live.com/invoke/107522/Visual%20Studio%202010%20editor%20vs.%20gVim%20over%20Remote%20Desktop/iframe.html" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9897053" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/andrewarnottms/archive/tags/WPF/default.aspx">WPF</category><category domain="http://blogs.msdn.com/andrewarnottms/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>How to find the focused ListBoxItem in WPF</title><link>http://blogs.msdn.com/andrewarnottms/archive/2007/09/05/how-to-find-the-focused-listboxitem-in-wpf.aspx</link><pubDate>Wed, 05 Sep 2007 18:10:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4765438</guid><dc:creator>andarno</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/andrewarnottms/comments/4765438.aspx</comments><wfw:commentRss>http://blogs.msdn.com/andrewarnottms/commentrss.aspx?PostID=4765438</wfw:commentRss><description>&lt;p&gt;One would expect that the WPF ListBox control would have some kind of FocusedIndex property to find out which item has the keyboard focus (and that dashed border around it) -- but it's not there.&amp;nbsp; It seems the workaround requires&amp;nbsp;a few lines of code, which I present here.&lt;/p&gt; &lt;h5&gt;ListBox.SelectionMode = Single&lt;/h5&gt; &lt;p&gt;In a ListBox's default mode that allows exactly one item to be selected, you can quite easily query the SelectedIndex property instead, since the focused item is always also the one selected item.&amp;nbsp; &lt;/p&gt; &lt;h5&gt;ListBox.SelectionMode = Multiple || Extended&lt;/h5&gt; &lt;p&gt;But when you allow multiple selections, finding the focused item is not as simple.&amp;nbsp; It may not even be selected.&amp;nbsp; &lt;/p&gt; &lt;p&gt;There &lt;em&gt;is&lt;/em&gt; a ListBoxItem.IsFocused boolean property.&amp;nbsp; If you're &lt;em&gt;not&lt;/em&gt; using data-binding (you probably should be) you can just iterate through ListBox.Items and break out when you find where ListBoxItem.IsFocused == true.&lt;/p&gt; &lt;p&gt;If you &lt;em&gt;are&lt;/em&gt; using data-binding, then the ListBox.Items collection is your data-bound objects rather than ListBoxItems.&amp;nbsp; You have to get to your ListBoxItem instances another way:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; listBox1.Items.Count; i++) {
    &lt;span class="kwrd"&gt;object&lt;/span&gt; yourObject = listBox1.Items[i];
    ListBoxItem lbi = (ListBoxItem)listBox1.ItemContainerGenerator.ContainerFromItem(yourObject);
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (lbi.IsFocused) {
        MessageBox.Show(&lt;span class="str"&gt;"Item at index "&lt;/span&gt; + i.ToString() + &lt;span class="str"&gt;" has the focus."&lt;/span&gt;);
        &lt;span class="kwrd"&gt;break&lt;/span&gt;;
    }
}
&lt;/pre&gt;
&lt;h4&gt;So where are we at?&lt;/h4&gt;
&lt;p&gt;Even with these tricks, if the focus isn't on the ListBox at all, there's no programmatic way (that I can find anyway) to find out which ListBoxItem would have focus once the ListBox received focus again.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4765438" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/andrewarnottms/archive/tags/WPF/default.aspx">WPF</category></item></channel></rss>