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 8457943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:56:38+00:00 2026-06-10T12:56:38+00:00

I’ve been trying to expand Josh Smith’s demo MVVM application in order to better

  • 0

I’ve been trying to expand Josh Smith’s demo MVVM application in order to better understand the principals behind it and I’ve hit a wall when trying to implement a filter function on a View using a ListView.

I have spent a few hours researching and dabbling but its just not working.

My first step was to bind a textbox in my view to a property in my ViewModel:

<TextBox Height="25" Name="txtFilter" Width="150" Text="{Binding Path=Filter, UpdateSourceTrigger=PropertyChanged}"/>

This matches in my VM:

public string Filter
    {
        get { return this.filter; }
        set
        {
            this.filter = value;
            OnFilterChanged();
        }
    }

My VM used a ObservableCollection for the datasource but I’ve tried to convert it into an ICollectionView after reading tutorials:

internal ObservableCollection<StaffViewModel> InnerStaff { get; set; }
    internal CollectionViewSource CvsStaff { get; set; }
    public ICollectionView AllStaff
    {
        get { return CvsStaff.View; }
    }

In my constructor I have specified:

CvsStaff = new CollectionViewSource();
CvsStaff.Source = this.InnerStaff;
CvsStaff.Filter += ApplyFilter;

When my Filter Property gets updated it calls OnFilterChanged which is:

private void OnFilterChanged()
    {
        CvsStaff.View.Refresh();
    }

My ApplyFilter Function is:

void ApplyFilter(object sender, FilterEventArgs e)
    {
        StaffViewModel svm = (StaffViewModel)e.Item;

        if (this.Filter.Length == 0)
        {
            e.Accepted = true;
        }
        else
        {
            e.Accepted = svm.LastName.Contains(Filter);
        }
    }

Is there a silly mistake that I’ve made that anyone can help me spot? I’m fairly new to WPF and the MVVM pattern so I’m still learning!

EDIT

In the View I bind the collection with:

<CollectionViewSource
  x:Key="StaffGroup"
  Source="{Binding Path=AllStaff}"
  />

and the ListView is as such:

<ListView
      Name="staffList"
      AlternationCount="2" 
      DataContext="{StaticResource StaffGroup}" 
      ItemContainerStyle="{StaticResource StaffItemStyle}"
      ItemsSource="{Binding}"
        Grid.Row="1">
  • 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-06-10T12:56:40+00:00Added an answer on June 10, 2026 at 12:56 pm

    The binding is incorrect. You need to make a few changes. The first thing is to make sure that the DataContext is set correctly. Typically you’ll do this on a parent of the ListView and not set it directly on the ListView control. This could be a UserControl / Window / etc.

    So assuming you have a view model:

    public class MainViewModel
    {
        public MainViewModel()
        {
            //Create some fake data 
            InnerStaff = new ObservableCollection<StaffViewModel>();
            InnerStaff.Add(new StaffViewModel {FirstName = "Sue", LastName = "Bucknell"});
            InnerStaff.Add(new StaffViewModel {FirstName = "James", LastName = "Bucknell"});
            InnerStaff.Add(new StaffViewModel {FirstName = "John", LastName = "Harrod"});
    
            CvsStaff = new CollectionViewSource();
            CvsStaff.Source = this.InnerStaff;
            CvsStaff.Filter += ApplyFilter;
        }
    
        private string filter;
    
        public string Filter
        {
            get { return this.filter; }
            set
            {
                this.filter = value;
                OnFilterChanged();
            }
        }
    
        private void OnFilterChanged()
        {
            CvsStaff.View.Refresh();
        }
    
        internal ObservableCollection<StaffViewModel> InnerStaff { get; set; }
        internal CollectionViewSource CvsStaff { get; set; }
        public ICollectionView AllStaff
        {
            get { return CvsStaff.View; }
        }
    
        void ApplyFilter(object sender, FilterEventArgs e)
        {
            StaffViewModel svm = (StaffViewModel)e.Item;
    
            if (string.IsNullOrWhiteSpace(this.Filter) || this.Filter.Length == 0)
            {
                e.Accepted = true;
            }
            else
            {
                e.Accepted = svm.LastName.Contains(Filter);
            }
        }
    }
    

    And assuming you have a Window MainWindow.cs (code behind) you could (for this example) hook up the DataContext here.

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }
    }
    

    Then you have a few choices for doing your binding, you could specify your CollectionViewSource in XAML or in code, but you’ve done both. i.e. the xaml one, with the key x:key=”StaffGroup” and the VM one CvsStaff. Let’s say we get rid of the xaml one completely and use the VM one, which is setup correctly. Then you would bind using the ItemsSource property, like so:

    <ListView Name="staffList" 
          AlternationCount="2" 
          ItemsSource="{Binding AllStaff}" 
          Grid.Row="1" />
    

    Also small thing, I’ve changed the Filter to check for nulls and whitespace. You may also need to change it to be case-insensitive.

    One other thing that I haven’t mentioned here but is crucial is to implement INotifyPropertyChanged on your StaffViewModel – I assume you have, if not here’s some code. You would typically also do this on most of your view models, to notify the view of changes to properties.

    internal class StaffViewModel : INotifyPropertyChanged
    {
        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set
            {
                _firstName = value;
                OnPropertyChanged("FirstName");
            }
        }
    
        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
                OnPropertyChanged("LastName");
            }
        }
        public override string ToString()
        {
            return string.Format("{0} {1}", FirstName, LastName);
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.