I have an application written in wpf, which downloads some webpages, parses 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 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 ListOfItems
class is method which does all the job (there are some other methods used inside to make code readable) - downloads source, parses and fills ListToBind
with data, f.e.
[0] => NameOfItem = "FirstOne", Properties = {99, #FF00FF00}
[1] => NameOfItem = "SecondOne", Properties = {50, #FFFF0000}
etc.
As you can see, when this method DownloadItems
completes its job, OnDownloadCompleted
event is raised. In the main thread is following code
void listOfItems_OnDownloadCompleted(object sender, EventArgs args)
{
dataGrid.Dispatcher.Invoke(new Action(() => {
dataGrid.ItemsSource = ListOfItemsInstance.ListToBind;
}));
}
DataGrid on the MainWindow.xaml
is filled with values, because of 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 just fine. But there is this problem. Try to uncomment commented xaml snippet and you will get Must create DependencySource on same Thread as the DependencyObject.
error.
Finally, my question is, how to avoid this error?
EDIT:
It should look like this in the end. This picture is taken from MS Excel and coloured in Adobe Photoshop.
The SolidColorBrush is a Freezable which is a derived DispatcherObject. DispatcherObjects have thread affinity - i.e it can only be used/interacted with on the thread on which it was created. Freezables however do offer the ability to freeze an instance. This will prevent any further changes to the object but it will also release the thread affinity. So you can either change it so that your property is not storing a DependencyObject like SolidColorBrush and instead just store the Color. Or you can freeze the SolidColorBrush that you are creating using the Freeze method.