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

  • Home
  • SEARCH
  • 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 6553725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:36:58+00:00 2026-05-25T12:36:58+00:00

I have a nice WPF DataGrid binded to a collection of objects. Everything works

  • 0

I have a nice WPF DataGrid binded to a collection of objects. Everything works fine, when the properties of any object change the grid is updated. The problem is that the rows are not re-sorted when the update happens and then the sort is not valid anymore.

Any idea on how to fix that?

Thanks in advance.

EDIT: This is how I’m binding the DataGrid:

<Controls:DataGrid MinHeight="300" MinWidth="300" ItemsSource="{Binding Data}" AutoGenerateColumns="True">

public class MainWindowViewModel
{ 
    public ObservableCollectionMultiThread<StockViewModel> Data { get; private set; }
} 

public class ObservableCollectionMultiThread<T> : ObservableCollection<T>
{
    // Override the event so this class can access it
    public override event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;

    protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        // Be nice - use BlockReentrancy like MSDN said
        using (base.BlockReentrancy())
        {
            System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler = this.CollectionChanged;
            if (eventHandler == null)
                return;

            Delegate[] delegates = eventHandler.GetInvocationList();
            // Walk thru invocation list
            foreach (System.Collections.Specialized.NotifyCollectionChangedEventHandler handler in delegates)
            {
                DispatcherObject dispatcherObject = handler.Target as DispatcherObject;
                // If the subscriber is a DispatcherObject and different thread
                if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
                {
                    // Invoke handler in the target dispatcher's thread
                    dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e);
                }
                else // Execute handler as is
                    handler(this, e);
            }
        }
    }
}

PD: I’m using the DataGrid from CTP October 2008 as I’m with Net 3.5

  • 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-25T12:36:59+00:00Added an answer on May 25, 2026 at 12:36 pm

    Usually controls on WPF does not respond to item edition. I’ve used this class as the source for the grid:

    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Windows.Data;
    using System.Windows.Threading;
    
    namespace StockCrawler
    {
    public class AutoRefreshListCollectionView : ListCollectionView
    {
    
        public AutoRefreshListCollectionView(IList list)
            : base(list)
        {
            this.SubscribeSourceEvents(list, false);
        }
    
        private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
    
            bool refresh = false;
    
            foreach (SortDescription sort in this.SortDescriptions)
            {
                if (sort.PropertyName == e.PropertyName)
                {
                    refresh = true;
                    break;
                }
            }
    
            if (!refresh)
            {
                foreach (GroupDescription group in this.GroupDescriptions)
                {
                    PropertyGroupDescription propertyGroup = group as PropertyGroupDescription;
    
                    if (propertyGroup != null && propertyGroup.PropertyName == e.PropertyName)
                    {
                        refresh = true;
                        break;
                    }
                }
            }
    
            if (refresh)
            {
                if (!this.Dispatcher.CheckAccess())
                {
                    // Invoke handler in the target dispatcher's thread
                    this.Dispatcher.Invoke((Action)this.Refresh); 
                }
                else // Execute handler as is
                {
                    this.Refresh();                 
                }
            }
        }
    
        private void Source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                this.SubscribeItemsEvents(e.NewItems, false);
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                this.SubscribeItemsEvents(e.OldItems, true);
            }
            else
            {
                // TODO: Support this
    
            }
        }
    
        private void SubscribeItemEvents(object item, bool remove)
        {
            INotifyPropertyChanged notify = item as INotifyPropertyChanged;
    
            if (notify != null)
            {
                if (remove)
                {
                    notify.PropertyChanged -= this.Item_PropertyChanged;
                }
                else
                {
                    notify.PropertyChanged += this.Item_PropertyChanged;
                }
            }
        }
    
        private void SubscribeItemsEvents(IEnumerable items, bool remove)
        {
            foreach (object item in items)
            {
                this.SubscribeItemEvents(item, remove);
            }
        }
    
        private void SubscribeSourceEvents(object source, bool remove)
        {
            INotifyCollectionChanged notify = source as INotifyCollectionChanged;
    
            if (notify != null)
            {
                if (remove)
                {
                    notify.CollectionChanged -= this.Source_CollectionChanged;
                }
                else
                {
                    notify.CollectionChanged += this.Source_CollectionChanged;
                }
            }
    
            this.SubscribeItemsEvents((IEnumerable)source, remove);
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nice page that does everything I need. However one of the
I have a WPF Datagrid control. I want to apply style for scrollbars. It
i have a problem with wpf-grid in .net-framework. I have defined a grid in
I have a listbox that is databound to a Collection of objects. The listbox
I have a wpf datagrid (.NET 4.0) that contains raw data from a database,
What I have so far is: A WPF application, using MVVM, IDataErrorInfo implemented. Everything
I have a nice and lovely Django site up and running, but have noticed
I have a nice DataGridView showing what is basically some kind of log data
I have this nice little MSBuild-based daily build setup that I use on my
I have a nice interface, and I want to implement one member of it

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.