RSS Feed


WPF: How to filter ListBox items on the fly

Here's a simple example that I created to show how to make a ListBox control that has many items more manageable. As you type into the filter TextBox, the ListBox will only show the items that matches the filter.  You can give the sample a try by clicking the XBAP link below.

http://blogs.vertigosoftware.com/Files/Alan/FilteredListBox/FilteredListBox.xbap

image

The magic happens in code by retrieving the default view for the ListBox's ItemsSource and applying a filter against it.

private void FilterTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
   
//Use collection view to filter the listbox
    ObjectDataProvider peopleDS = (ObjectDataProvider)this.Resources["PeopleDS"];
   
ICollectionView collectionView = CollectionViewSource.GetDefaultView(peopleDS.ObjectInstance);
    collectionView.Filter =
new Predicate<object>(NameFilter);
}

public bool NameFilter(object item)
{
   
Person person = item as Person;
   
return (person.DisplayName.ToLower().Contains(FilterTextBox.Text.ToLower()));
}

Here is the full source code:

http://blogs.vertigosoftware.com/Files/Alan/src/filteredlistbox.zip

 
Posted by Alan Le | 34 Comments | Trackback Url | Bookmark with:        
Tags:

Links to this Post

Comments

Monday, 4 Feb 2008 05:48 by Nice
<div class=ExternalClassBA396176CC2F40A7AF65B1EED6497301><div>Alan,</div> <div> </div> <div>Nice post.  Thanks for sharing this with us.</div> <div> </div> <div>Cheers,</div> <div> </div> <div>Karl</div></div>

Friday, 8 Aug 2008 01:32 by npnuogef
<div class=ExternalClass9FA16402D21D4177853BCEC56E5655E4> <a href="http://ymoomhyf.com">rrxtzjgb</a> [URL=http://udqojmot.com]psdsowlf[/URL] ilzlqqwm http://egxtlvys.com mcavrkee yuytegru </div>

Sunday, 10 Aug 2008 02:55 by buy adipex online myoblastoma dislensing
<div class=ExternalClassEB1202C9B4C14BA79EFCFBC0F0015D24>I don't mind what language an opera is sung in so long as it is a language I don't understand.</div>

Sunday, 10 Aug 2008 06:17 by ionamin
<div class=ExternalClass0ADD1F1C2BAC4E2E9EE215B2772EAB9A>Happiness lies in the joy of achievement and the thrill of creative effort.</div>

Sunday, 10 Aug 2008 10:05 by prilosec aluminosis secessionist
<div class=ExternalClass33879B65E9844BA6904063410E38281D>Nor do not saw the air too much with your hand, thus, but use all gently. For in the very torrent, tempest, and as I may say, whirlwind of passion, you must acquire and beget a temperance that may give it smoothness.</div>

Monday, 11 Aug 2008 01:46 by celebrex
<div class=ExternalClass51ACA2C82BA64537904EF21359822231>Marriage is the only adventure open to the cowardly.</div>

Monday, 11 Aug 2008 05:45 by lansoprazole
<div class=ExternalClassEDF49B7398B048208A48FCDE12681B6D>To lengthen thy life, lessen thy meals.</div>

Monday, 11 Aug 2008 09:14 by order carisoprodol online
<div class=ExternalClassB05D4037BAC243EEA28E4B1B5E67E788>There is a great deal of difference between an eager man who wants to read a book and the tired man who wants a book to read.</div>

Monday, 11 Aug 2008 01:21 by order valium
<div class=ExternalClassE945B3BF27834AAE8CA0BB10226295CB>I was not a child prodigy, because a child prodigy is a child who knows as much when it is a child as it does when it grows up.</div>

Monday, 11 Aug 2008 05:09 by order cialis online
<div class=ExternalClassA50FEB18D7C2434DAC10A84A6DF23F05>Dance is the hidden language of the soul.</div>

Monday, 11 Aug 2008 08:38 by ambien online
<div class=ExternalClassADB6B6E751214A35A436605519D01C83>Forget injuries, never forget kindnesses.</div>

Tuesday, 12 Aug 2008 12:37 by cheap cialis online
<div class=ExternalClass453CBF99EE32434BAECB3D1E93BA6800>Beware the pull on your heartstrings -- it's often the pursestrings that are actually being reached for.</div>

Tuesday, 12 Aug 2008 04:20 by zolpidem
<div class=ExternalClassF660F87209584E3AAADE212D73F012AE>Good judgment comes from experience, and experience comes from bad judgment.</div>

Tuesday, 12 Aug 2008 08:18 by buy levitra
<div class=ExternalClass9C02D53A5501441393F7048A18A16B17>Silent gratitude isn't much use to anyone.</div>

Tuesday, 12 Aug 2008 11:58 by order tramadol
<div class=ExternalClass6DF297ADB7F0475094A3D56FABE7B33D>I like work: it fascinates me. I can sit and look at it for hours.</div>

Wednesday, 13 Aug 2008 06:58 by yrardxin
<div class=ExternalClass44125BBE7D5748CCB4818BD6420F6AED> <a href="http://xgmqudey.com">iygrpanx</a> [URL=http://pjzccifj.com]sagaedjh[/URL] ecbtxglp http://giypkpqq.com cdeqqsnx bdyuweuf </div>

Wednesday, 13 Aug 2008 08:15 by cheap valium
<div class=ExternalClass8941A2D5B2744C17A54A890FF5415AFF>For me, it's that I contributed, ... That I'm on this planet doing some good and making people happy. That's to me the most important thing, that my hour of television is positive and upbeat and an antidote for all the negative stuff going on in life.</div>

Wednesday, 13 Aug 2008 11:57 by phentermine
<div class=ExternalClass975B4DC884AF40FEA39C481124FDE733>Inside myself is a place where I live all alone and that's where you renew your springs that never dry up.</div>

Wednesday, 13 Aug 2008 03:38 by giddily
<div class=ExternalClass5EBC1EF7F38B4238949606DCD13C2CFE>Computer Science is no more about computers than astronomy is about telescopes.</div>

Wednesday, 13 Aug 2008 07:06 by ciprofloxacin
<div class=ExternalClassF712B18DBC0B4593A5E489EACA6BA205>This world is given as the prize for the men in earnest; and that which is true of this world, is truer still of the world to come.</div>

Wednesday, 13 Aug 2008 10:25 by order soma online
<div class=ExternalClass79C92CA50D954536AC50792B24B54F5E>We do not believe if we do not live and work according to our belief.</div>

Thursday, 14 Aug 2008 02:10 by azithromycin
<div class=ExternalClass4804E89204B94C3498B6D85113788B75>I ran the wrong kind of business, but I did it with integrity.</div>

Thursday, 14 Aug 2008 05:45 by cheap viagra online
<div class=ExternalClass35E22336F09C45819A9E40D5CC1D2A51>I sent the club a wire stating, PLEASE ACCEPT MY RESIGNATION. I DON'T WANT TO BELONG TO ANY CLUB THAT WILL ACCEPT ME AS A MEMBER.</div>

Thursday, 14 Aug 2008 09:32 by generic tadalafil
<div class=ExternalClass5D3ABDC41CD6479188FD7CB6CE289202>I love being married. It's so great to find that one special person you want to annoy for the rest of your life.</div>

Thursday, 14 Aug 2008 01:21 by buy carisoprodol online
<div class=ExternalClassCEE0F3F6666F4EDE8AD7BBA2ADFAD2B4>The real hero is always a hero by mistake; he dreams of being an honest coward like everybody else.</div>

Thursday, 14 Aug 2008 04:44 by cheap carisoprodol
<div class=ExternalClass814C467DAFA04DA38EE472FEB80F57EC>When all is said and done, the weather and love are the two elements about which one can never be sure.</div>

Thursday, 14 Aug 2008 08:10 by order viagra online superintend cannabis
<div class=ExternalClass8EFDE90183914C1C8308E73D06AC0463>Intimacy is being seen and known as the person you truly are.</div>

Friday, 15 Aug 2008 07:52 by ylfoqcjx
<div class=ExternalClassBF064D30FE6D4BA3A238F5FFAA7C653E> phlrjywp http://aaefakaa.com bhjutjbx egxhpygz [URL=http://cyslbydm.com]lvdeaums[/URL] <a href="http://nitictcg.com">tcomgbwx</a> </div>

Friday, 15 Aug 2008 01:02 by aleve
<div class=ExternalClassFF0B9DDF5B234A2B85A6076CAC67E15E>I like an escalator because an escalator can never break, it can only become stairs. There would never be an escalator temporaly out of order sign, only an escalator temporarily stairs. Sorry for the convenience.</div>

Friday, 15 Aug 2008 04:43 by trazodone
<div class=ExternalClass5AEEDAD765FF43C1843DB026757C0466>Beginning today, treat everyone you meet as if they were going to be dead by midnight. Extend them all the care, kindness and understanding you can muster. Your life will never be the same again.</div>

Friday, 15 Aug 2008 08:04 by propecia online
<div class=ExternalClass65D93D91869E4C5A838406C03C99764A>For visions come not to polluted eyes.</div>

Friday, 15 Aug 2008 11:29 by atenolol
<div class=ExternalClass372C9A3C324C464CBB608906CEC2F734>A man can do all things if he but wills them.</div>

Thursday, 18 Sep 2008 12:12 by Casino
<div class=ExternalClassEB3250B5187D45FAA072FB22F1F01ED2> The New Dress summary and study guide, with notes, essays, quotes, and pictures. </div>

Monday, 22 Sep 2008 05:09 by Poker
<div class=ExternalClass00AC0B36E62E4D84ADD6D12F5998312C> Information about cell phones for the US Detailed info on new phones, news </div>

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation