Dispatcher.Invoke fails

92 Views Asked by At

I'm stuck at a problem when updating my View.

I have two DataGrids on which I have to add columns dynamically. To do that, I've added an Attached Property to the DataGrid that binds to an ObservableCollection<DataGridColumn> and then wrote a custom ObjectToDataGridColumnConverter that converts an object (mostly strings) to a DataGridColumn and then adds this new column to the DataGrid.

It works fine with one of my two DataGrids. The other one (which is updated first) breaks on

var grid = source as DataGrid;
grid.Columns.Add(col);

with an InvalidOperationException:

The calling thread cannot access this object because a different thread owns it.

Even if I try Application.Current.Dispatcher.Invoke(new Action(() => grid.Columns.Add(col)) it breaks with the same exception.

My other DataGrid uses the same code to add its columns and it works fine. Even without any Invoke calls to the Dispatcher object.

0

There are 0 best solutions below