Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>On what dispatcher instance are you calling <code>Invoke</code> ?</p> <p>I'm guessing it is the Dispatcher from the background thread that is executing <code>CreateModel</code>, not the one from the UI thread.</p> <p>The DataGrid is a control, and so derives from <code>DispatcherObject</code>. Each such object exposes the dispatcher of its owner thread through its <code>Dispatcher</code> property, this is the one you should use to call methods on the control.</p> <p>Changing the dispatcher in the call should work:</p> <pre><code>internal CreateModel() { Model d = new Model(); // Invoke the action on the dispatcher of the DataGrid MainDataGrid.Dispatcher.Invoke( DispatcherPriority.Normal, new Action&lt;Model&gt;( delegate(Model d1) { mModel = d1; // mModel is a property defined in Window Binding b = new Binding(); b.Source = mModel; MainDataGrid.SetBinding(TreeView.ItemsSourceProperty, mainb); } } </code></pre> <p>You could also store the Dispatcher of the UI thread in a field before executing the background action, but using the Dispatcher of the control better shows the intentions of the code: "I want to call this on whatever thread this control belongs to".</p> <p><strong>Update</strong>: I just realized that this is an instance method of your control, so the dispatcher instance you are using is the right one. So much for answering late in the night. Also, your code works for me, replacing your model by an IEnumerable. Is there something special in your model ?</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