Having read a recent question on the App Hub Forums I decided to update my previous post on CollectionViewSource http://babaandthepigman.wordpress.com/2011/07/03/wp7-collectionviewsource-sorting-a-listbox/ to enable filtering.
So, I added a text box to enter the filter string,
And the following code to implement the filtering…
private void FilterBoxTextChanged(object sender, TextChangedEventArgs e)
{
Source.View.Filter = o =>
{
var item = o as Item;
if (item != null)
{
return item.Text.Contains(filterBox.Text);
}
return false;
};
}and we get filtered results…
Here’s the updated project https://skydrive.live.com/redir.aspx?cid=4f1b7368284539e5&resid=4F1B7368284539E5!436&parid=4F1B7368284539E5!123
I tried this code but a NullreferenceException is generated. Then I checked and found that Source.View is null.
why it is null ? do we need to set view property ? if yes how ?
I was having this problem as well using this method in a ViewModel rather than in the page and found that the main problem was due to newing and setting .Source in the VM ctor. I moved these few lines of code to the get accessor of the public CollectionViewSource property and it eliminated the exception.
However, filtering is not working still; the items are enumerated and set to a bool value of true is returned for the correct items in the ObservableCollection, but they seem to be ignored, as the list is not filtered. Even calling CollectionViewSource.View.Refresh() makes no difference. :/ Any suggestions?
Does this work with selectionchanged event on the list box?
@Peter Daukintis This solution is very slow on the phone? Do you know how to fix this ?
I have one question.
I use the methode which is a basic app that you can select bystarting a new project. It is named DataBount or …Bound. But i have no idea how i can use your solution for my app.
Please help me. I’m sure that is a long time when you write this but i need your help.