<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dasher's Corner &#187; dynamic filtering</title>
	<atom:link href="http://dasher.wordpress.com/tag/dynamic-filtering/feed/" rel="self" type="application/rss+xml" />
	<link>http://dasher.wordpress.com</link>
	<description>Natural Language &#38; Machine Translation</description>
	<lastBuildDate>Sat, 20 Jun 2009 13:08:28 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='dasher.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/333fa50f89d3d9a1b2eb9aeb6cc7b342?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Dasher's Corner &#187; dynamic filtering</title>
		<link>http://dasher.wordpress.com</link>
	</image>
			<item>
		<title>WPF: Dynamic Filtering Data &#8211; the simple approach</title>
		<link>http://dasher.wordpress.com/2007/11/25/wpf-dynamic-filtering-data-the-simple-approach/</link>
		<comments>http://dasher.wordpress.com/2007/11/25/wpf-dynamic-filtering-data-the-simple-approach/#comments</comments>
		<pubDate>Sun, 25 Nov 2007 17:58:46 +0000</pubDate>
		<dc:creator>Dasher</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[dynamic filtering]]></category>
		<category><![CDATA[filtering data]]></category>

		<guid isPermaLink="false">http://dasher.wordpress.com/2007/11/25/wpf-dynamic-filtering-data-the-simple-approach/</guid>
		<description><![CDATA[You often find yourself wanting to quickly filter data based on a criteria defined by a user in an application.  A classic example is filtering what&#8217;s shown in a listbox by what a user has typed into a textbox.
There are many approaches to solving the problem but a quick and simple approach is: (first the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dasher.wordpress.com&blog=86394&post=79&subd=dasher&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>You often find yourself wanting to quickly filter data based on a criteria defined by a user in an application.  A classic example is filtering what&#8217;s shown in a listbox by what a user has typed into a textbox.</p>
<p>There are many approaches to solving the problem but a quick and simple approach is: (first the xaml):</p>
<pre><span class="kwrd">&lt;</span><span class="html">Window</span> <span class="attr">x:Class</span><span class="kwrd">="Window1"</span> <span class="kwrd">
  </span><span class="attr">xmlns</span><span class="kwrd">=http://schemas.microsoft.com/winfx/2006/xaml/presentation</span> <span class="kwrd"></span><span class="attr">
  xmlns:x</span><span class="kwrd">=http://schemas.microsoft.com/winfx/2006/xaml</span> <span class="kwrd"></span>
  <span class="attr">xmlns:local</span><span class="kwrd">="clr-namespace:contactManager"</span> <span class="kwrd">
  </span><span class="attr">xmlns:system</span><span class="kwrd">="clr-namespace:System;assembly=mscorlib"</span>
  <span class="attr">Title</span><span class="kwrd">="Window1"</span> <span class="attr">Height</span><span class="kwrd">="598"</span> <span class="attr">Width</span><span class="kwrd">="1015"</span><span class="kwrd">&gt;
  </span><span class="kwrd">&lt;</span><span class="html">Window.Resources</span><span class="kwrd">&gt;
  </span>   <span class="kwrd">&lt;</span><span class="html">local:contacts</span> <span class="attr">x:Key</span><span class="kwrd">="myContacts"</span> <span class="kwrd">/&gt;</span>    
     <span class="kwrd">&lt;</span><span class="html">CollectionViewSource</span> <span class="attr">x:Key</span><span class="kwrd">="contactsViewSource"</span>
         <span class="attr">Source</span><span class="kwrd">="{StaticResource myContacts}"</span> <span class="attr">Filter</span><span class="kwrd">="CollectionViewSource_Filter"</span> <span class="kwrd">/&gt;
</span>
<span class="kwrd">  &lt;/</span><span class="html">Window.Resources</span><span class="kwrd">&gt;
  </span><span class="kwrd">&lt;</span><span class="html">Grid</span> <span class="attr">Name</span><span class="kwrd">="Grid1"</span><span class="kwrd">&gt;
    </span><span class="kwrd">&lt;</span><span class="html">Grid.RowDefinitions</span><span class="kwrd">&gt;
      </span><span class="kwrd">&lt;</span><span class="html">RowDefinition</span> <span class="attr">Height</span><span class="kwrd">="60*"</span> <span class="kwrd">/&gt;
      </span><span class="kwrd">&lt;</span><span class="html">RowDefinition</span> <span class="attr">Height</span><span class="kwrd">="423*"</span> <span class="kwrd">/&gt;
    </span><span class="kwrd">&lt;/</span><span class="html">Grid.RowDefinitions</span><span class="kwrd">&gt;
    </span><span class="kwrd">&lt;</span><span class="html">TextBox</span> <span class="attr">Margin</span><span class="kwrd">="62,26,16,0"</span> <span class="attr">Name</span><span class="kwrd">="txtSearch"</span> <span class="attr">Height</span><span class="kwrd">="23"</span> <span class="attr">VerticalAlignment</span><span class="kwrd">="Top"
    </span> <span class="attr">AcceptsReturn</span><span class="kwrd">="True"</span> <span class="attr">FontStyle</span><span class="kwrd">="Italic"</span> <span class="attr">KeyUp</span><span class="kwrd">="txtSearch_KeyUp"</span> <span class="kwrd">/&gt;
    </span><span class="kwrd">&lt;</span><span class="html">Label</span> <span class="attr">Margin</span><span class="kwrd">="12,12,66,0"</span> <span class="attr">Name</span><span class="kwrd">="Label1"</span> <span class="attr">Height</span><span class="kwrd">="28"</span> <span class="attr">VerticalAlignment</span><span class="kwrd">="Top"</span><span class="kwrd">&gt;</span>Search<span class="kwrd">&lt;/</span><span class="html">Label</span><span class="kwrd">&gt;
</span>
<span class="kwrd">    &lt;</span><span class="html">ListBox</span> <span class="attr">Grid</span>.<span class="attr">Row</span><span class="kwrd">="1"</span> <span class="attr">Name</span><span class="kwrd">="lstContacts"</span> <span class="attr">IsSynchronizedWithCurrentItem</span><span class="kwrd">="True"</span><span class="attr"> 
      ItemsSource</span><span class="kwrd">="{Binding Source={StaticResource contactsViewSource}}"</span> <span class="kwrd">/&gt;
  </span><span class="kwrd">&lt;/</span><span class="html">Grid</span><span class="kwrd">&gt;</span>
<span class="kwrd">
&lt;/</span><span class="html">Window</span><span class="kwrd">&gt;</span></pre>
<p>The local resource <em>contacts</em> is a class that is defined in the application.  The namespace is imported into the XAML with <em>&#8216;<span class="attr">xmlns:local</span><span class="kwrd">=&#8221;clr-namespace:contactManager</span>&#8220;&#8216;.  </em>The class itself is referenced in XAML by <em>&#8216;<span class="kwrd">&lt;</span><span class="html">local:contacts</span> <span class="attr">x:Key</span><span class="kwrd">=&#8221;myContacts&#8221;</span> <span class="kwrd">/&gt;</span>&#8216;.</em></p>
<p>The class itself inherits from an observableCollection &#8211; although the approach would work with any datasource that can be bound to the listbox. After contacts has been imported &#8211; we setup a collectionViewSource &#8211; rather than using the default viewSource provided by WPF.</p>
<p>The code behind to connect the filter text with the filter applied to the listbox is:</p>
<pre>Partial <span class="kwrd">Public</span> <span class="kwrd">Class</span> Window1
  <span class="kwrd">Private</span> <span class="kwrd">Function</span> findString(<span class="kwrd">ByVal</span> haystack <span class="kwrd">As</span> <span class="kwrd">String</span>, <span class="kwrd">ByVal</span> needle <span class="kwrd">As</span> <span class="kwrd">String</span>, _
                                 <span class="kwrd">Optional</span> <span class="kwrd">ByVal</span> comparison <span class="kwrd">As</span> StringComparison _
                                    = StringComparison.CurrentCultureIgnoreCase) <span class="kwrd">As</span> <span class="kwrd">Integer
</span>    ' We are using string.indexOf rather than string.contains as string.contains
    ' doesn't allow us to specify CurrentCultureIgnoreCase
    <span class="kwrd">Return</span> haystack.IndexOf(needle, 0, comparison) 

  <span class="kwrd">End</span> <span class="kwrd">Function</span> 

<span class="kwrd">Private</span> <span class="kwrd">Sub</span> CollectionViewSource_Filter(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> System.<span class="kwrd">Object</span>, _
                                    <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.Windows.Data.FilterEventArgs)
   ' Cast the listbox item to the type used
<span class="kwrd">  Dim</span> thisContact <span class="kwrd">As</span> contact = <span class="kwrd">CType</span>(e.Item, contact) 

  <span class="kwrd">If</span> (thisContact IsNot <span class="kwrd">Nothing</span>) <span class="kwrd">Then</span>  ' Handle an empty item 

    <span class="kwrd">If</span> <span class="kwrd">String</span>.IsNullOrEmpty(txtSearch.Text) <span class="kwrd">Then</span>
      ' Make sure we have some text that we wish to filter

      e.Accepted = <span class="kwrd">True</span>
      ' If we don't then we want all items to appear in the listbox

    <span class="kwrd">Else</span>
      ' findString returns the index of the needle (search text) in
      ' the haystack (text to be searched)
      ' IndexOf returns -1 if the needle isn't found in the haystack 

      <span class="kwrd">If</span> (findString(thisContact.firstName, txtSearch.Text) &gt; -1) _
               <span class="kwrd">Or</span> (findString(thisContact.lastName, txtSearch.Text) &gt; -1) <span class="kwrd">Then</span> 

        ' The needle exists in the haystack - so accept this entry
        e.Accepted = <span class="kwrd">True</span> 

      <span class="kwrd">Else</span>
        ' This item doesn't contain the needle we are looking for

        e.Accepted = <span class="kwrd">False</span>

      <span class="kwrd">End</span> <span class="kwrd">If</span> 

    <span class="kwrd">End</span> <span class="kwrd">If</span>

  <span class="kwrd">Else</span>
    ' thisContact IS nothing - so don't bother displaying it 

    e.Accepted = <span class="kwrd">False</span> 

  <span class="kwrd">End</span> <span class="kwrd">If</span> 

<span class="kwrd">End</span> <span class="kwrd">Sub</span> 

<span class="kwrd">Private</span> <span class="kwrd">Sub</span> txtSearch_KeyUp(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> System.<span class="kwrd">Object</span>, _
<span class="kwrd">                                ByVal</span> e <span class="kwrd">As</span> System.Windows.Input.KeyEventArgs)
' This event will be raised whenever the keyUp event occurs on the
' textbox that we are using to filter
' Do a simple refresh on the listbox 

    <span class="kwrd">CType</span>(<span class="kwrd">Me</span>.FindResource(<span class="str">"contactsViewSource"</span>), CollectionViewSource).View.Refresh() 

  <span class="kwrd">End</span> <span class="kwrd">Sub</span> 

<span class="kwrd">End</span> <span class="kwrd">Class</span> 

  And that's it - a listbox containing items is filtered based on the text typed by the user in a search box.</pre>
<p style="display:inline;margin:0;padding:0;" class="wlWriterSmartContent">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/wpf">wpf</a>,<a rel="tag" href="http://technorati.com/tags/livesearch">livesearch</a>,<a rel="tag" href="http://technorati.com/tags/dynamic%20filtering">dynamic filtering</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dasher.wordpress.com/79/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dasher.wordpress.com/79/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dasher.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dasher.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dasher.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dasher.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dasher.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dasher.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dasher.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dasher.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dasher.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dasher.wordpress.com/79/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dasher.wordpress.com&blog=86394&post=79&subd=dasher&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dasher.wordpress.com/2007/11/25/wpf-dynamic-filtering-data-the-simple-approach/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/be5e0c2700dfad20c5981478273d4853?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dasher</media:title>
		</media:content>
	</item>
	</channel>
</rss>