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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:50:08+00:00 2026-06-02T21:50:08+00:00

I want adding items to DataGrid in thread. I have view model for my

  • 0

I want adding items to DataGrid in thread.

I have view model for my user control:

public class Contact
{
    public string Name { get; set; }
    public string Phone { get; set; }
}

public class ContactGridViewModel
{
    public ContactGridViewModel()
    {
        Items = new ObservableCollection<Contact>();
    }

    public ObservableCollection<Contact> Items { get; private set; }

    public Dispatcher Dispatcher
    {
        get { return Dispatcher.CurrentDispatcher); }
    }
    private DispatcherOperation LastOperation { get; set; }

    public void Update(IEnumerable<Contact> contacts)
    {
        if (LastOperation != null) LastOperation.Abort();

        Items.Clear();
        foreach (var item in contacts)
            LastOperation = Dispatcher.BeginInvoke(
                    DispatcherPriority.Background,
                    new ParameterizedThreadStart(o => Items.Add(o))),
                    item);
    }
}

There is the markup example:

<!--DataContext="{Binding ContactGridViewModel}-->

<DataGrid ItemsSource="{Binding Items, Mode=OneWay}">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
    <DataGridTextColumn Header="Phone" Binding="{Binding Path=Phone}" />
  </DataGrid.Columns>
</DataGrid>

After Update method is executed second time, and if first collection is not completely added, DataGrid will contains part of first collection and full second collection.

// contactList1 and contactList2 are IEnumerable<Contact> objects
Model.Update(contactList1);
Model.Update(contactList2);

I try to abort DispatcherOperation, but it’s not stop adding contacts into Items property.

Could you tell me the correct method of adding objects to DataGrid ItemsSource?

  • 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-02T21:50:11+00:00Added an answer on June 2, 2026 at 9:50 pm

    You should collect all DispatcherOpretations in list and then abort them all.

    public class ContactGridViewModel
    {
        public ContactGridViewModel()
        {
            Items = new ObservableCollection<Contact>();
            LastOperations = new List<DispatcherOperation>();
        }
    
        public ObservableCollection<Contact> Items { get; private set; }
        private ICollection<DispatcherOperation> LastOperations { get; set; }
    
        public Dispatcher Dispatcher
        {
            get { return Dispatcher.CurrentDispatcher); }
        }
    
        private void StopUpdating()
        {
            foreach (var operation in LastOperations)
            {
                operation.Abort();
            }
            LastOperations.Clear();
        }
    
        public void Update(IEnumerable<Contact> contacts)
        {
            StopUpdating();
    
            Items.Clear();
            foreach (var item in contacts)
                LastOperations.Add(Dispatcher.BeginInvoke(
                        DispatcherPriority.Background,
                        new ParameterizedThreadStart(o => Items.Add(item))),
                        item));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a thread adding items to a BlockingCollection . On another thread I
I have a storage class that has events on adding items to it. My
I have a user control that contains a combobox. I want to be able
I want to remove a css class before adding a new class to an
I am adding the LinearLayout(child view) to another LinearLayout(parentview) programatically here I want to
I have a SVG file that I want to extend by adding onclick handlers
I am adding a new blank row to a dojox.grid.DataGrid, and want to focus
I have a list List<MyObject> myList and I am adding items to a list
i have problem with simpleXml and adding new items. This is my xml: <?xml
I have a WPF datagrid that is filled with an ObserverableCollection. Now I want

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.