Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6054241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:07:51+00:00 2026-05-23T08:07:51+00:00

I am working on a WPF desktop application using the MVVM pattern. I am

  • 0

I am working on a WPF desktop application using the MVVM pattern.

I am trying to filter some items out of a ListView based on the text typed in a TextBox. I want the ListView items to be filtered as I change the text.

I want to know how to trigger the filter when the filter text changes.

The ListView binds to a CollectionViewSource, which binds to the ObservableCollection on my ViewModel. The TextBox for the filter text binds to a string on the ViewModel, with UpdateSourceTrigger=PropertyChanged, as it should be.

<CollectionViewSource x:Key="ProjectsCollection"
                      Source="{Binding Path=AllProjects}"
                      Filter="CollectionViewSource_Filter" />

<TextBox Text="{Binding Path=FilterText, UpdateSourceTrigger=PropertyChanged}" />

<ListView DataContext="{StaticResource ProjectsCollection}"
          ItemsSource="{Binding}" />

The Filter="CollectionViewSource_Filter" links to an event handler in the code behind, which simply calls a filter method on the ViewModel.

Filtering is done when the value of FilterText changes – the setter for the FilterText property calls a FilterList method that iterates over the ObservableCollection in my ViewModel and sets a boolean FilteredOut property on each item ViewModel.

I know the FilteredOut property is updated when the filter text changes, but the List does not refresh. The CollectionViewSource filter event is only fired when I reload the UserControl by switching away from it and back again.

I’ve tried calling OnPropertyChanged("AllProjects") after updating the filter info, but it did not solve my problem.
(“AllProjects” is the ObservableCollection property on my ViewModel to which the CollectionViewSource binds.)

How can I get the CollectionViewSource to refilter itself when the value of the FilterText TextBox changes?

Many thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:07:51+00:00Added an answer on May 23, 2026 at 8:07 am

    Don’t create a CollectionViewSource in your view. Instead, create a property of type ICollectionView in your view model and bind ListView.ItemsSource to it.

    Once you’ve done this, you can put logic in the FilterText property’s setter that calls Refresh() on the ICollectionView whenever the user changes it.

    You’ll find that this also simplifies the problem of sorting: you can build the sorting logic into the view model and then expose commands that the view can use.

    EDIT

    Here’s a pretty straightforward demo of dynamic sorting and filtering of a collection view using MVVM. This demo doesn’t implement FilterText, but once you understand how it all works, you shouldn’t have any difficulty implementing a FilterText property and a predicate that uses that property instead of the hard-coded filter that it’s using now.

    (Note also that the view model classes here don’t implement property-change notification. That’s just to keep the code simple: as nothing in this demo actually changes property values, it doesn’t need property-change notification.)

    First a class for your items:

    public class ItemViewModel
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    

    Now, a view model for the application. There are three things going on here: first, it creates and populates its own ICollectionView; second, it exposes an ApplicationCommand (see below) that the view will use to execute sorting and filtering commands, and finally, it implements an Execute method that sorts or filters the view:

    public class ApplicationViewModel
    {
        public ApplicationViewModel()
        {
            Items.Add(new ItemViewModel { Name = "John", Age = 18} );
            Items.Add(new ItemViewModel { Name = "Mary", Age = 30} );
            Items.Add(new ItemViewModel { Name = "Richard", Age = 28 } );
            Items.Add(new ItemViewModel { Name = "Elizabeth", Age = 45 });
            Items.Add(new ItemViewModel { Name = "Patrick", Age = 6 });
            Items.Add(new ItemViewModel { Name = "Philip", Age = 11 });
    
            ItemsView = CollectionViewSource.GetDefaultView(Items);
        }
    
        public ApplicationCommand ApplicationCommand
        {
            get { return new ApplicationCommand(this); }
        }
    
        private ObservableCollection<ItemViewModel> Items = 
                                         new ObservableCollection<ItemViewModel>();
    
        public ICollectionView ItemsView { get; set; }
    
        public void ExecuteCommand(string command)
        {
            ListCollectionView list = (ListCollectionView) ItemsView;
            switch (command)
            {
                case "SortByName":
                    list.CustomSort = new ItemSorter("Name") ;
                    return;
                case "SortByAge":
                    list.CustomSort = new ItemSorter("Age");
                    return;
                case "ApplyFilter":
                    list.Filter = new Predicate<object>(x => 
                                                      ((ItemViewModel)x).Age > 21);
                    return;
                case "RemoveFilter":
                    list.Filter = null;
                    return;
                default:
                    return;
            }
        }
    }
    

    Sorting kind of sucks; you need to implement an IComparer:

    public class ItemSorter : IComparer
    {
        private string PropertyName { get; set; }
    
        public ItemSorter(string propertyName)
        {
            PropertyName = propertyName;    
        }
        public int Compare(object x, object y)
        {
            ItemViewModel ix = (ItemViewModel) x;
            ItemViewModel iy = (ItemViewModel) y;
    
            switch(PropertyName)
            {
                case "Name":
                    return string.Compare(ix.Name, iy.Name);
                case "Age":
                    if (ix.Age > iy.Age) return 1;
                    if (iy.Age > ix.Age) return -1;
                    return 0;
                default:
                    throw new InvalidOperationException("Cannot sort by " + 
                                                         PropertyName);
            }
        }
    }
    

    To trigger the Execute method in the view model, this uses an ApplicationCommand class, which is a simple implementation of ICommand that routes the CommandParameter on buttons in the view to the view model’s Execute method. I implemented it this way because I didn’t want to create a bunch of RelayCommand properties in the application view model, and I wanted to keep all the sorting/filtering in one method so that it was easy to see how it’s done.

    public class ApplicationCommand : ICommand
    {
        private ApplicationViewModel _ApplicationViewModel;
    
        public ApplicationCommand(ApplicationViewModel avm)
        {
            _ApplicationViewModel = avm;
        }
    
        public void Execute(object parameter)
        {
            _ApplicationViewModel.ExecuteCommand(parameter.ToString());
        }
    
        public bool CanExecute(object parameter)
        {
            return true;
        }
    
        public event EventHandler CanExecuteChanged;
    }
    

    Finally, here’s the MainWindow for the application:

    <Window x:Class="CollectionViewDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:CollectionViewDemo="clr-namespace:CollectionViewDemo" 
            Title="MainWindow" Height="350" Width="525">
        <Window.DataContext>
            <CollectionViewDemo:ApplicationViewModel />
        </Window.DataContext>
        <DockPanel>
            <ListView ItemsSource="{Binding ItemsView}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn DisplayMemberBinding="{Binding Name}"
                                        Header="Name" />
                        <GridViewColumn DisplayMemberBinding="{Binding Age}" 
                                        Header="Age"/>
                    </GridView>
                </ListView.View>
            </ListView>
            <StackPanel DockPanel.Dock="Right">
                <Button Command="{Binding ApplicationCommand}" 
                        CommandParameter="SortByName">Sort by name</Button>
                <Button Command="{Binding ApplicationCommand}" 
                        CommandParameter="SortByAge">Sort by age</Button>
                <Button Command="{Binding ApplicationCommand}"
                        CommandParameter="ApplyFilter">Apply filter</Button>
                <Button Command="{Binding ApplicationCommand}"
                        CommandParameter="RemoveFilter">Remove filter</Button>
            </StackPanel>
        </DockPanel>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a WPF application, and I'm structuring it using the MVVM pattern.
I'm working on a WPF application and is using the Model-View-ViewModel pattern. The application
I'm working in WPF using the MVVM pattern, and generally things seem to be
I'm working on a desktop project using WPF, framework 3.5. This application has lots
OK, working on WPF(using MVVM) and came across a question, want some input. I
We've developed a desktop application using .NetFX3.5 which has some winforms and two WPF
I am working on a Wpf desktop application, whenever i run my application it
I'm working with WPF and EF 4.2 to build a desktop application. I have
I’m working on WPF based application. Environment is VS2008 SP1 with .NET 3.5 SP
I am working on a project where I have implemented MVVM for WPF desktop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.