Note that there are some explanatory texts on larger screens.

plurals
  1. POMust create DependencySource on same Thread as DependencyObject
    primarykey
    data
    text
    <p>I have an application written in wpf, which downloads some webpages, parses html code and saves some values.</p> <pre><code>class ListOfItems { public List&lt;SomeObject&gt; ListToBind; public void DownloadItems() { Task.Factory.StartNew(() =&gt; { ... ... if (OnDownloadCompleted != null) OnDownloadCompleted(this, EventArgs.Empty); } } } class SomeObject { public string NameOfItem; public MyClass Properties; } class MyClass { public int Percentage; public SolidColorBrush Color; } </code></pre> <p>This is the object model I'm using. It's simplified version and I don't want you to reorganize it, there is a reason I wrote it this way. In <code>ListOfItems</code> class is method which does all the job (there are some other methods used inside to make code readable) - downloads source, parses and fills <code>ListToBind</code> with data, f.e.</p> <pre><code>[0] =&gt; NameOfItem = "FirstOne", Properties = {99, #FF00FF00} [1] =&gt; NameOfItem = "SecondOne", Properties = {50, #FFFF0000} etc. </code></pre> <p>As you can see, when this method <code>DownloadItems</code> completes its job, <code>OnDownloadCompleted</code> event is raised. In the main thread is following code</p> <pre><code>void listOfItems_OnDownloadCompleted(object sender, EventArgs args) { dataGrid.Dispatcher.Invoke(new Action(() =&gt; { dataGrid.ItemsSource = ListOfItemsInstance.ListToBind; })); } </code></pre> <p>DataGrid on the <code>MainWindow.xaml</code> is filled with values, because of following xaml code snippet.</p> <pre><code>&lt;DataGrid Name="dataGrid" AutoGenerateColumns="False"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Tag" Binding="{Binding Name}"/&gt; &lt;DataGridTextColumn Header="Color" Binding="{Binding MyClass.Percentage}"&gt; &lt;!--&lt;DataGridTextColumn.CellStyle&gt; &lt;Style TargetType="DataGridCell"&gt; &lt;Setter Property="Background" Value="{Binding MyClass.Color}" /&gt; &lt;/Style&gt; &lt;/DataGridTextColumn.CellStyle&gt;--&gt; &lt;/DataGridTextColumn&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre> <p>It works just fine. But there is this problem. Try to uncomment commented xaml snippet and you will get <code>Must create DependencySource on same Thread as the DependencyObject.</code> error.</p> <p>Finally, my question is, how to avoid this error?</p> <p><strong>EDIT:</strong></p> <p>It should look like this in the end. This picture is taken from MS Excel and coloured in Adobe Photoshop.</p> <p><img src="https://i.stack.imgur.com/7uDyr.jpg" alt="example"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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