1

I'm wondering if it is possible add children asynchronously to stackpanel (or any other element). It seems that all items are rendered at once.

it doesn't matter if i use background worker or dispatcher. Always the result is the same - when all items have been added then they are shown.

What i would like to have is something like this:

Deployment.Current.Dispatcher.BeginInvoke(() => {
    foreach (var item in items.Skip(x).Take(pageSize))
    {
        // when this row is executed new item should be visible
        ItemsList.Children.Add(new _ListItem(item));
    }
});

regards max

2 Answers 2

4

This snippet adds new TextBlock into stackPanel every 500 ms without freezing UI. Item is visible in UI right after it has been added:

ThreadPool.QueueUserWorkItem(_ => {
                foreach (int item in Enumerable.Range(1,50)) {
                  Thread.Sleep(500);//simulate some calculations here
                  int item1 = item;
                  Deployment.Current.Dispatcher.BeginInvoke(() => {
                      stackPanel.Children.Add(new TextBlock(){Text = "Text "+item1});
                  });
                }
            });
0
1

You should use the ObservableCollection class to store the items you want to display. That class implements the INotifyCollectionChanged interface, which will allow it to update the UI as items are added and removed.

Here are some more details about binding to an ObservableCollection and updating the UI as items are added (See this demo).

Also you may need to add/remove items using the Dispatcher.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.