Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To be straight to the point, creating new instance of the <code>ObservableCollection</code> every time your data changes then re-assigning it to the <code>ItemSource</code>, is not only disregarding core purpose of the <code>ObservableCollection</code>, it is also breaking/misusing the concept of <code>Data Binding</code>.</p> <pre><code>//this is the line I am talking about: var Items = new ObservableCollection&lt;Item&gt;(notUploadedItems); </code></pre> <p>Instead, create the <code>ObserbaleCollection</code> once -- make it a property:</p> <pre><code>private readonly ObservableCollection&lt;Item&gt; _items = new ObservableCollection&lt;Item&gt;(); public ObservableCollection&lt;Item&gt; Items {get {return _items;} </code></pre> <p>For the updates use collection's '.Add()' or 'Remove()' methods</p> <p>You're correct that you must be on the UI Main thread to do the updates:</p> <pre><code> DoItOnUIThread(() =&gt;Items.Add(newItem)); //here's a sample of method that would take any action and execute it on UI thread.. private void DoItOnUIThread(Action action) { if (_dispatchService == null) _dispatchService = ServiceLocator.Current.GetInstance&lt;IDispatchService&gt;(); if (_dispatchService.CheckAccess()) action.Invoke (); else _dispatchService.Invoke(action); } </code></pre> <p>Let the observable collection do its job, which is automatically update the UI through Binding with new items and their property changes. If all that is hooked up/done properly your listbox shouldn't need to be updated in cs code at all.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload