As the title suggests, I'm having an issue using a filter with an ICollectionView.
Basically I have a TextBox. When ever I type a letter into this TextBox, The filter is triggered (Thanks to INotifyPropertyChanged). As a result, the content the ICollectionView displays is narrowed down to match what's been typed. Or rather, it narrows down the content displayed in what's databound to the ICollectionVIew. i.e ListBox, DataGrid...
Below is a property for a TextBox that utilises the filter is bound to.
// This is a property bound to a TextBox meant to Search the First Name of a person
private string _searchFirstName
public string SearchFirstName
{
get
{
return _searchFirstName;
}
set
{
_searchFirstName= value;
OnPropertyChanged("SearchFirstName");
// After any changes have been made, the filter is fired.
PrincipalView.Filter = PrincipalFilter;
}
}
Then comes the Filter that's fired by the property.
private bool PrincipalFilter(object item)
{
// principal is the name of the table back in my relational database (MySQL)
principal principal = item as principal;
return principal.FirstName.Contains(SearchFirstName)
}
You see the above works fine for the simple reason being that everybody has a first name. As thus with every record / row entered within the table principal, there will never be a null or empty cell in the FirstName column. However, the same cannot be said for a column for second name, or WorkPhone.
So when I add a "SearchSecondName" property for instance, where not everyone in the table has a second name (ie. some of the cells in the actual corresponding table column are empty), I get a NullReferenceException thrown every time in the actual filter. So I cannot filter out the ICollectionView based on those fields, even if it's necessary.
I would really like to be able to filter information using these columns but can't seem to figure out a way around it. Any assistance on getting around this would be greatly appreciated.