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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:06:26+00:00 2026-06-06T23:06:26+00:00

I wish to recalculate things everytime a DataGrid gets more rows or some are

  • 0

I wish to recalculate things everytime a DataGrid gets more rows or some are removed. I tried to use the Loaded event, but that was fired only once.

I found AddingNewItem, but that is fired before it has been added. I need to do my stuff afterwards.

There’s also LayoutUpdated, which works, but I’m afraid it’s not wise to use it, because it fires way too often for my purposes.

  • 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-06T23:06:30+00:00Added an answer on June 6, 2026 at 11:06 pm

    If your DataGrid is bound to something, I think of two ways of doing this.

    You could try getting the DataGrid.ItemsSource collection, and subscribing to its CollectionChanged event. This will only work if you know what type of collection it is in the first place.

    // Be warned that the `Loaded` event runs anytime the window loads into view,
    // so you will probably want to include an Unloaded event that detaches the
    // collection
    private void DataGrid_Loaded(object sender, RoutedEventArgs e)
    {
        var dg = (DataGrid)sender;
        if (dg == null || dg.ItemsSource == null) return;
    
        var sourceCollection = dg.ItemsSource as ObservableCollection<ViewModelBase>;
        if (sourceCollection == null) return;
    
        sourceCollection .CollectionChanged += 
            new NotifyCollectionChangedEventHandler(DataGrid_CollectionChanged);
    }
    
    void DataGrid_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // Execute your logic here
    }
    

    The other solution would be to use an Event System such as Microsoft Prism’s EventAggregator or MVVM Light’s Messenger. This means your ViewModel would broadcast a DataCollectionChanged event message anytime the bound collection changes, and your View would subscribe to receive these messages and execute your code anytime they occur.

    Using EventAggregator

    // Subscribe
    eventAggregator.GetEvent<CollectionChangedMessage>().Subscribe(DoWork);
    
    // Broadcast
    eventAggregator.GetEvent<CollectionChangedMessage>().Publish();
    

    Using Messenger

    //Subscribe
    Messenger.Default.Register<CollectionChangedMessage>(DoWork);
    
    // Broadcast
    Messenger.Default.Send<CollectionChangedMessage>()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to use a join across some tables but am having some difficulty
I wish I could have a more helpful title for this question, but honestly
I wish to add the mouseover events to a panel which contains more than
I wish to edit ini files over web server, decided to use django, been
I wish HTML could do something semantically equivalent to this; <dl class=main-list> <definitionitem> <dt>Some
I wish to iterate over a set but the contents of the set will
Wish to know more about the practical differences between init and initWithNibName . SO
I wish to draw circle but when I fill() my arc all other shapes
I wish to contribute code to http://svn.r-project.org/R/ But I only have experience with using
I wish to create a sticky header. Everytime that the user scrolls down AND

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.