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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:21:37+00:00 2026-05-18T01:21:37+00:00

I am using WPF, and attempting to follow the MVVM pattern. Our team has

  • 0

I am using WPF, and attempting to follow the MVVM pattern. Our team has decided to use the Xceed DataGrid control, and I am having some difficulties getting it to fit into the MVVM pattern.

One requirement that I have to meet is that I need to know when a user changes a column filter on the grid. I am aware that the latest version of the DataGrid control has an event that is raised for this, but unfortunatly, I have to use an older version of the control.

After searching for awhile, I found this post. It says that I need to hook a INotifyCollectionChanged handler to each of the possible list of filters. This works, but it also says that I need to unhook the handlers whenever the row source of the grid changes.

I was able to get it to work when I explicitly set the row source in the codebehind of the page (and in my first attempt in the ModelView using a direct reference to the view gasp!)

The first problem that I run into though, is how to do this without having the logic in the code behind or in the ViewModel. My solution was to extend the DataGridControl class and to add the following code:

    private IDictionary<string, IList> _GridFilters = null;
    public MyDataGridControl() : base()
    {
        TypeDescriptor.GetProperties(typeof(MyDataGridControl))["ItemsSource"].AddValueChanged(this, new EventHandler(ItemsSourceChanged));
    }

    void ItemsSourceChanged(object sender, EventArgs e)
    {
        UnsetGridFilterChangedEvent();
        SetGridFilterChangedEvent();
    }

    public void SetGridFilterChangedEvent()
    {
        if (this.ItemsSource == null)
            return;

        DataGridCollectionView dataGridCollectionView = (DataGridCollectionView)this.ItemsSource;

        _GridFilters = dataGridCollectionView.AutoFilterValues;

        foreach (IList autofilterValues in _GridFilters.Values)
        {
            ((INotifyCollectionChanged)autofilterValues).CollectionChanged += FilterChanged;
        }
    }

    /*TODO: Possible memory leak*/
    public void UnsetGridFilterChangedEvent()
    {
        if (_GridFilters == null)
            return;

        foreach (IList autofilterValues in _GridFilters.Values)
        {
            INotifyCollectionChanged notifyCollectionChanged = autofilterValues as INotifyCollectionChanged;

            notifyCollectionChanged.CollectionChanged -= FilterChanged;
        }

        _GridFilters = null;
    }

This lead me to my next problem; I’m pretty sure that by the time the ItemsSourceChanged method is called, the collection of AutoFilterValues has already changed, so I can not effectively unhook the handlers.

Am I right in assuming this? And can anyone think of a better way of managing these handlers while still allowing me to keep that functionality encapsulated within my extended class?

Sorry about the length of the post, and thanks in advance for the help!

-Funger

  • 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-18T01:21:38+00:00Added an answer on May 18, 2026 at 1:21 am

    You are correct that AutoFilterValues will have already changed at that point, so you will be unhooking the wrong handlers, resulting in a memory leak.

    The solution is very easy. Do exactly what you are doing but use an List<IList> instead of just referencing AutoFilterValues:

    private List<IList> _GridFilters;
    

    and use ToList() to make a copy of the filters you set handlers on:

    _GridFilters = dataGridCollectionView.AutoFilterValues.Values.ToList();
    

    Since _GridFilters is now a List<IList>, you’ll also have to change the loops:

    foreach(IList autofilterValues in _GridFilters) 
      ...
    

    The reason this works is that the actual list of old filter lists is copied into _GridFilters, rather than simply referencing the AutoFilterValues property.

    This is a nice general technique that is applicable in many situations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using WPF, I have a TreeView control that I want to set its ItemTemplate
I'm using a WPF WebBrowser control to preview HTML typed by the user. example...
I'm using the WPF 3.5SP1 WebBrowser control to display a page containing some javascript
Greetings, I'm using WPF with a Model-View-ViewModel pattern, and I have a view model
I am attempting to build a simple WPF Application using msbuild via a custom
I've got a Windows Forms control that I'm attempting to wrap as a WPF
I'm currently attempting to print a document from WPF. I'm using the web browser
I am using Wpf Toolkit DataGrid. Whenever I assign Itemssource to it, its first
I am using wpf framework 3.5 for this I had to use winworms ->
I'm developing a system using WPF 4. I pretend to use MVC in development.

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.