{"id":443,"date":"2007-09-05T11:10:21","date_gmt":"2007-09-05T11:10:21","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/andrewarnottms\/2007\/09\/05\/how-to-find-the-focused-listboxitem-in-wpf\/"},"modified":"2019-04-03T22:35:32","modified_gmt":"2019-04-04T05:35:32","slug":"how-to-find-the-focused-listboxitem-in-wpf","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/how-to-find-the-focused-listboxitem-in-wpf\/","title":{"rendered":"How to find the focused ListBoxItem in WPF"},"content":{"rendered":"<p>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) &#8212; but it&#8217;s not there.&nbsp; It seems the workaround requires&nbsp;a few lines of code, which I present here.<\/p>\n<h5>ListBox.SelectionMode = Single<\/h5>\n<p>In a ListBox&#8217;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.&nbsp; <\/p>\n<h5>ListBox.SelectionMode = Multiple || Extended<\/h5>\n<p>But when you allow multiple selections, finding the focused item is not as simple.&nbsp; It may not even be selected.&nbsp; <\/p>\n<p>There <em>is<\/em> a ListBoxItem.IsFocused boolean property.&nbsp; If you&#8217;re <em>not<\/em> using data-binding (you probably should be) you can just iterate through ListBox.Items and break out when you find where ListBoxItem.IsFocused == true.<\/p>\n<p>If you <em>are<\/em> using data-binding, then the ListBox.Items collection is your data-bound objects rather than ListBoxItems.&nbsp; You have to get to your ListBoxItem instances another way:<\/p>\n<p><!-- code formatted by http:\/\/manoli.net\/csharpformat\/ --><\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">for<\/span> (<span class=\"kwrd\">int<\/span> i = 0; i &lt; listBox1.Items.Count; i++) {\r\n    <span class=\"kwrd\">object<\/span> yourObject = listBox1.Items[i];\r\n    ListBoxItem lbi = (ListBoxItem)listBox1.ItemContainerGenerator.ContainerFromItem(yourObject);\r\n    <span class=\"kwrd\">if<\/span> (lbi.IsFocused) {\r\n        MessageBox.Show(<span class=\"str\">\"Item at index \"<\/span> + i.ToString() + <span class=\"str\">\" has the focus.\"<\/span>);\r\n        <span class=\"kwrd\">break<\/span>;\r\n    }\r\n}\r\n<\/pre>\n<h4>So where are we at?<\/h4>\n<p>Even with these tricks, if the focus isn&#8217;t on the ListBox at all, there&#8217;s no programmatic way (that I can find anyway) to find out which ListBoxItem would have focus once the ListBox received focus again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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) &#8212; but it&#8217;s not there.&nbsp; It seems the workaround requires&nbsp;a few lines of code, which I present here. ListBox.SelectionMode = Single In a ListBox&#8217;s default [&hellip;]<\/p>\n","protected":false},"author":2685,"featured_media":37840,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[4617,396],"class_list":["post-443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-permierdev","tag-andarno","tag-wpf"],"acf":[],"blog_post_summary":"<p>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) &#8212; but it&#8217;s not there.&nbsp; It seems the workaround requires&nbsp;a few lines of code, which I present here. ListBox.SelectionMode = Single In a ListBox&#8217;s default [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/2685"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=443"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/443\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}