In my Window constructor after InitializeComponents, I need to create an object and bind it to a datagrid. Since the object creation is taking too much time, the windows takes a while to show up. So I decided to move the creation of the object to a background thread and "delegate back" to the UI thread by doing a dispatcher.invoke to perform the binding. But this fails.
The weird thing is if I try to set the visibility of a rectangle I have inside the Dispatcher.invoke, that works but the DataGrid.setbinding doesnt! Any ideas? I have tried the same thing with background worker, and threadstart, but I keep getting the same error. I am not able to access the DataGrid object even though its happening inside the dispatcher invoke delegate. Am sure am missing something in my understanding of how this works. Any suggestions would be greatly appreciated. Thanks!
StartupDelegate s = new StartupDelegate(CreateModel);
s.BeginInvoke(delegate(IAsyncResult aysncResult) { s.EndInvoke(aysncResult); }, null);
internal CreateModel()
{
Model d = new Model();
Dispatcher.Invoke( DispatcherPriority.Normal,
new Action<Model>(
delegate(Model d1)
{
mModel = d1; // mModel is a property defined in Window
Binding b = new Binding();
b.Source = mModel;
MainDataGrid.SetBinding(TreeView.ItemsSourceProperty, mainb); // << dies here with - The calling thread cannot access this object because a different thread owns it.
}
}
UPDATE: Ended up using a dispatchertimer that would run just once. Putting the binding code in its Tick delegate worked. But I am still curious why the above code doesnt.