I have a datagrid. I want to add columns as a result of an event. So I do
for (int iii = 1; iii <= 4; ++iii)
{
var dtgColumn = new DataGridTextColumn();
dtgColumn.Header = "AAA"
Dispatcher.Invoke((Action)(() => { dtgResults.Columns.Add(dtgColumn); }));
}
But despite using a dispatcher I get this error:
The calling thread cannot access this object because a different thread owns it.
Thank you for any help Patrick }
It looks like a problem not a
UI
control itself, butdtgColumn
object created. You are creatingUI
element on one thread and add it to theUI
element on the main thread.Change your code like:
So the object is created and added on the thread that owns
UI
parent control.