Created
October 27, 2015 03:18
-
-
Save LGM-AdrianHum/1fe6a4ea4ea9ea8f9403 to your computer and use it in GitHub Desktop.
Bind a TextBox to filter a listbox...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TextSearchFilter { | |
public TextSearchFilter(ICollectionView filteredView, TextBox textBox) { | |
string filterText = ""; | |
filteredView.Filter = delegate(object obj) { | |
if (String.IsNullOrEmpty(filterText)) return true; | |
string str = obj as string; | |
if (String.IsNullOrEmpty(str)) return false; | |
int index = str.IndexOf( | |
filterText, | |
0, | |
StringComparison.InvariantCultureIgnoreCase); | |
return index > -1; | |
}; | |
textBox.TextChanged += delegate { | |
filterText = textBox.Text; | |
filteredView.Refresh(); | |
}; | |
} | |
} | |
//The ViewModel unfortunately will need to know about the textbox... | |
string[] wordList = this.FindResource("WordList") as string[]; | |
ICollectionView view = CollectionViewSource.GetDefaultView(wordList); | |
new TextSearchFilter(view, this.txtSearch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment