I'm creating a application that allows a user to add some Employee details into EntityFramework
model using WPF.
So far, I have a ListView
to represent a list of employee names, and when you select the name of the employee, it selects that specific data in another ListView
. I have accomplished this using a Predicate
and a ICollectionSource
.
But what I want to achieve now, is to have a so called search engine. So when a user types in an employees name in a TextBox
it filters the names of the employee names, depending on what is typed into the search box.
I have used This Link as a guide, but I am not too sure how to implement it within my own design; in the example they have used a Resource
and used an Array
.
This is what I have tried instead, using a Predicate
;
private EmployeeListViewModel()
: base("")
{
EmployeeList = new ObservableCollection<EmployeeViewModel>(GetEmployees());
this._view = new ListCollectionView(this.employeeList);
}
private ListCollectionView _view;
public ICollectionView View
{
get { return this._view; }
}
private string _TextSearch;
public string TextSearch
{
get { return _TextSearch; }
set
{
_TextSearch = value;
OnPropertyChanged("TextSearch");
if (String.IsNullOrEmpty(value))
View.Filter = null;
else
View.Filter = new Predicate<object>(o => ((EmployeeViewModel)o).FirstName == value);
}
}
in my view;
<TextBox Height="23" Name="txtSearch" VerticalAlignment="Bottom" Margin="70,0,0,183" Width="100" Grid.Row="1"
Text="{Binding TextSearch, UpdateSourceTrigger=PropertyChanged}"/>
But what seems to happen is when I type something in, it throws this exception;
Object reference not set to an instance of an object.
So my question is, how can I implement this so it actually enables me to filter the list of names like in a searchbox?
Any help would be grateful or guidance how to achieve this.
View
contains elements or is it maybe empty? And isEmployeeList
really the encapsulation foremployeeList
? Also isFirstName
a string maybe you should just for test set a default value for it – WiiMaxx Mar 18 '13 at 11:24employeeList
gets a list of employees and is bound to aListview
(mentioned briefly in question).FirstName
is a string, which is a property from a child VM. If it helps, I could post the whole ViewModel code to give you a better understanding of what is going on. – greg Mar 18 '13 at 12:03