{"id":3733,"date":"2016-05-04T07:18:55","date_gmt":"2016-05-04T07:18:55","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3733"},"modified":"2016-05-04T07:18:55","modified_gmt":"2016-05-04T07:18:55","slug":"filtering-listbox-data-in-wpf","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/filtering-listbox-data-in-wpf\/","title":{"rendered":"Filtering listbox data in WPF"},"content":{"rendered":"<p>Every now and then we&#8217;ll need to display items in a ListBox (or other ItemsControl) which we can filter. <\/p>\n<p>One might simply create two ObservableCollections, one containing all items and the other being the filtered items. Then bind the ItemsSource from the ListBox to the filtered items list.<\/p>\n<p>A simple alternate would be to use a CollectionView, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyViewModel\r\n{\r\n   public MyViewModel()\r\n   {\r\n      Unfiltered = new ObservableCollection&lt;string&gt;();\r\n      Filtered = CollectionViewSource.GetDefaultView(Unfiltered);\r\n   }\r\n\r\n   public ObservableCollection&lt;string&gt; Unfiltered { get; private set; }\r\n   public ICollectionView Filtered { get; private set; }\r\n}\r\n<\/pre>\n<p>Now to filter the collection view we can use the following (this is a silly example which will filter to show only strings larger than 3 in length)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nFiltered.Filter = i =&gt; ((string)i).Length &gt; 3;\r\n<\/pre>\n<p>to remove the filter we can just assign null to it, thus<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nFiltered.Filter = null;\r\n<\/pre>\n<p>In use, all we need to do is bind our <em>Filtered<\/em> property, for example in a ListBox control&#8217;s ItemsSource property and then apply a filter or remove a filter as required. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every now and then we&#8217;ll need to display items in a ListBox (or other ItemsControl) which we can filter. One might simply create two ObservableCollections, one containing all items and the other being the filtered items. Then bind the ItemsSource from the ListBox to the filtered items list. A simple alternate would be to use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-3733","post","type-post","status-publish","format-standard","hentry","category-wpf"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3733"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3733\/revisions"}],"predecessor-version":[{"id":3900,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3733\/revisions\/3900"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}