I've seen lots of similar questions but I've not been able to find either a question/answer or tutorial which clearly lists out all of the components required to get this to work. I'm trying to follow MVVM but as this is entirely a UI concern I'm not against doing some code-behind.
What I am trying to achieve:
ListView.ItemsSource
bound to anObservableCollection<T>
- Filter the displayed items in
ListView
based on aTextBox
- Filter is updated as user types in
TextBox
In my ViewModel
I have something like this:
private ObservableCollection<Customer> _customers;
public ObservableCollection<Customer> Customers
{
get { return _customers; }
set
{
_customers= value;
RaisePropertyChanged("Customers");
}
}
private Customer _selected_Customer;
public Customer Selected_Customer
{
get { return _selected_Customer; }
set
{
_selected_Customer= value;
RaisePropertyChanged("Selected_Customer");
}
}
private string _filtered_Name;
public string Filtered_Name
{
get { return _filtered_Name; }
set
{
_filtered_Name = value;
RaisePropertyChanged("Filtered_Name");
}
}
And in my XAML it's like this:
<CollectionViewSource x:Key="cvs"
x:Name="Customer_Details_View"
Source="{Binding Path=Customers}"/>
<TextBox x:Name="Filtered_Name" Text="{Binding Filtered_Name, Mode=TwoWay}"/>
<ListView ItemsSource="{Binding ElementName=Customer_Details_View}"
SelectedItem="{Binding Selected_Customer, Mode=TwoWay}">
I want to filter my ObservableCollection<Customer>
with the following logic: Customer.Name.ToLower().Contains(Filtered_Name.ToLower())
How to I bind the TextBox.Text
to the CollectionViewSource
or utilize the CollectionViewSource.Filter
event to apply the above filter?