Using LinQ to Filter ObservableCollection
I have an MVVM application and I am trying to do filtering through LinQ on my ObservableCollection retrieved from an Entity Framework based database.
In the view model, I have the following:
public class MenuListViewModel : BaseViewModelCollection<Menu>
{
private string filterString;
public string FilterString
{
get { return filterString; }
set
{
if (Equals(value, filterString)) return;
filterString = value;
RaisePropertyChanged();
}
}
//TODO problems with notification, filter doesn't work
public ObservableCollection<Menu> FilteredItems
{
get
{
if (filterString == null) return Items; //Items is Observable Collection that contains every Item
var query = Items.Where(x => x.Time.ToString().StartsWith(filterString));
return new ObservableCollection<Menu>(query);
}
}
public MenuListViewModel(MenuService menuService)
{
base.Service = menuService; //Using IoC to get service
}
}
In Xaml, I have the following binding:
<TextBox x:Name="RecipeFilterBox" Margin="5,5,0,0" TextWrapping="Wrap" Text="{Binding FilterString, NotifyOnTargetUpdated=True}" Grid.Column="1" Height="47.07" VerticalAlignment="Top"/>
The thing is, when I write something in the TextBox, nothing changes. I know there is something wrong with the propertyChanged event, but I really can't figure out how to fix it. If you need more information about this app, just ask me.
EDIT: The Xaml for FilteredItems looks like this:
<ListBox x:Name="MenuItemsListView" ItemsSource="{Binding FilteredItems}" SelectedItem="{Binding DeletedItem, Mode=OneWayToSource}" Foreground="#FFFFEDD3" FontFamily="Segoe Print" FontWeight="Bold" FontSize="18.667" Grid.ColumnSpan="3" Grid.Row="1" ItemContainerStyle="{DynamicResource ListBoxItemStyle1}" Style="{DynamicResource ListBoxStyle1}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Recipe.Name}" Width="255"/>
<TextBlock Width="175" Text="{Binding Time, Converter={StaticResource EnumTimeToItsDescriptionValueConverter}, Mode=OneWay}" />
<TextBlock Text="{Binding Date, StringFormat=dd.MM.yyyy}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
source to share
you can achieve this using ICollectionView
.
use FilteredItems
as main source ICollectionView
and show ICollectionView
for your view insteadObservableCollection<Menu>
Use a filter delegate to provide filter logic
FilteredItems.Filter = item =>
{
Menu m = item as Menu;
return m.Time.ToString().StartsWith(FilterString);
}
and when FilterString
invoke changesFilterItems.Refresh();
Here's an example:
public class MenuListViewModel : BaseViewModelCollection<Menu>
{
public MenuListViewModel()
{
var data = new List<Menu> { some data ... }; // your real list of menus
// initialize the collection view
FilteredItems = CollectionViewSource.GetDefaultView(data);
// apply filtering delegate
FilteredItems.Filter = i =>
{
// This will be invoked for every item in the underlying collection
// every time Refresh is invoked
if (string.IsNullOrEmpty(FilterString)) return true;
Menu m = i as Menu;
return m.Time.ToString().StartsWith(FilterString);
};
}
private string filterString;
public string FilterString
{
get { return filterString; }
set
{
if (Equals(value, filterString)) return;
filterString = value;
FilteredItems.Refresh(); // tirggers filtering logic
RaisePropertyChanged("FilterString");
}
}
public ICollectionView FilteredItems { get; set; }
}
You will also have to change UpdateSourceTrigger
to your filter TextBox
so that it updates FilterString
every time the user changes the text.
Text="{Binding FilterString, UpdateSourceTrigger=PropertyChanged, ...}
source to share