
{"id":1413,"date":"2014-04-29T23:30:47","date_gmt":"2014-04-30T03:30:47","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1413"},"modified":"2014-04-29T23:30:47","modified_gmt":"2014-04-30T03:30:47","slug":"wpf-editable-combobox-text-disappears","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1413","title":{"rendered":"WPF: Editable ComboBox Text Disappears"},"content":{"rendered":"<p><code>ComboBox<\/code> looks like a simple thing, but it&#8217;s surprisingly hard to get right. Today I bumped into another bug (or feature) of the WPF combo-box. I am recording it here, so I don&#8217;t forget the details.<\/p>\n<p>Suppose you have a <code>ComboBox<\/code>. The user selects an item in the combo box. ComboBox&#8217;s <code>ItemSource<\/code> then changes. If previously selected item is still in the items list, nothing happens. However, if previously selected item is not in the list, the <code>Text<\/code> property will become an empty string. This is kind of makes sense for drop-down lists, but for editable combo-boxes it looks odd.<\/p>\n<p><b>Example:<\/b><\/p>\n<p>You use a combo box to display a list of countries. You have three country lists: for Europe, Asia, Africa, and <code>null<\/code> list. Let&#8217;s say you start with Europe and the user selects Russia. Then you switch the list to Asian countries. Russia is there too, so everything&#8217;s fine. Then you switch to African countries, and voila: user selection disappears.<\/p>\n<p>An unexpected feature is that if the combo-box is editable and the user types R-u-s-s-i-a instead of selecting an item from the drop-down, the text will <b>not<\/b> disappear and will remain even if you switch to an items list not containing Russia.<\/p>\n<p>I wrote a little attached behavior that prevents the combo-box from losing the text when previously selected item is no longer in the list. You use it like this:<\/p>\n<p><code><\/p>\n<pre>&lt;ComboBox IsEditable=\"True\" Text=\"{Binding Something}\" <b><font color=\"red\">local:ComboBoxUtils.ProtectText=\"True\"<\/b><\/font>\/&gt;<\/pre>\n<p><\/code><\/p>\n<p>The extension source code is found here: <a href=\"http:\/\/www.ikriv.com\/dev\/wpf\/ComboBoxText\/ComboBoxUtils.cs\">ComboBoxUtils.cs<\/a><\/p>\n<p>A complete sample program demonstrating how it works: <a href=\"http:\/\/www.ikriv.com\/dev\/wpf\/ComboBoxText\/ComboBoxText.zip\">ComboBoxText.zip<\/a><\/p>\n<p>Main view: <a href=\"http:\/\/www.ikriv.com\/dev\/wpf\/ComboBoxText\/MainWindow.xaml\">MainWindow.xaml<\/a><\/p>\n<p>The view model: <a href=\"http:\/\/www.ikriv.com\/dev\/wpf\/ComboBoxText\/MainViewModel.cs\">MainViewModel.cs<\/a><\/p>\n<p><a style=\"display:none\" href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=1181663\" rel=\"tag\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ComboBox looks like a simple thing, but it&#8217;s surprisingly hard to get right. Today I bumped into another bug (or feature) of the WPF combo-box. I am recording it here, <a href=\"https:\/\/ikriv.com\/blog\/?p=1413\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[12],"tags":[],"class_list":["entry","author-ikriv","post-1413","post","type-post","status-publish","format-standard","category-wpf"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1413"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1413\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}