Must create a DependencySource in the same topic as DependencyObject

I have an application written in wpf that loads some web pages, parses the html code and saves some values.

class ListOfItems { public List<SomeObject> ListToBind; public void DownloadItems() { Task.Factory.StartNew(() => { ... ... if (OnDownloadCompleted != null) OnDownloadCompleted(this, EventArgs.Empty); } } } class SomeObject { public string NameOfItem; public MyClass Properties; } class MyClass { public int Percentage; public SolidColorBrush Color; } 

This is the object model that I use. This is a simplified version, and I do not want you to reorganize it, there is a reason why I wrote it this way. In ListOfItems class is a method that performs the entire task (other methods are read to read the code) - loads the source, parses and populates the ListToBind data, fe

 [0] => NameOfItem = "FirstOne", Properties = {99, #FF00FF00} [1] => NameOfItem = "SecondOne", Properties = {50, #FFFF0000} etc. 

As you can see, when this DownloadItems method completes its task, the OnDownloadCompleted event is OnDownloadCompleted . In the main thread, the following code

 void listOfItems_OnDownloadCompleted(object sender, EventArgs args) { dataGrid.Dispatcher.Invoke(new Action(() => { dataGrid.ItemsSource = ListOfItemsInstance.ListToBind; })); } 

The DataGrid on MainWindow.xaml populated with values ​​due to the following xaml code snippet.

 <DataGrid Name="dataGrid" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="Tag" Binding="{Binding Name}"/> <DataGridTextColumn Header="Color" Binding="{Binding MyClass.Percentage}"> <!--<DataGridTextColumn.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="Background" Value="{Binding MyClass.Color}" /> </Style> </DataGridTextColumn.CellStyle>--> </DataGridTextColumn> </DataGrid.Columns> </DataGrid> 

It works great. But there is this problem. Try uncommenting the xaml comment and you get the Must create DependencySource on same Thread as the DependencyObject. error Must create DependencySource on same Thread as the DependencyObject. .

Finally, my question is: how to avoid this error?

EDIT:

In the end, it should look like this. This image is taken from MS Excel and painted in Adobe Photoshop.

example

+6
source share
2 answers

SolidColorBrush is Freezable , which is a DispatcherObject derived object. DispatcherObjects are thread bound - i.e. It can only be used / interacted with the thread on which it was created. However, Freezables offer the option to freeze an instance. This will prevent any further changes to the object, but also lead to the convergence of flows. Thus, you can either change it so that your DependencyObject object, for example SolidColorBrush, does not store your property, but instead simply stores the color. Or you can freeze the SolidColorBrush that you create using the Freeze method.

+22
source

I think the standard way is to output the data object from Freezable and Freeze before transferring it to another thread. After an object is frozen, you can no longer modify it, so there is no danger of cutting errors.

Another option would be to make the data object a simple C # object (not derived from DispatcherObject ) and implement INotifyPropertyChanged yourself.

+1
source

Source: https://habr.com/ru/post/899901/


All Articles