Skip to content

Instantly share code, notes, and snippets.

@LGM-AdrianHum
Created October 27, 2015 03:18
Show Gist options
  • Save LGM-AdrianHum/1fe6a4ea4ea9ea8f9403 to your computer and use it in GitHub Desktop.
Save LGM-AdrianHum/1fe6a4ea4ea9ea8f9403 to your computer and use it in GitHub Desktop.
Bind a TextBox to filter a listbox...
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