c# - WPF: Passing objects between UI Thread and Background thread -
After InitializeComponents in my window creator, I can create an object and bind it to a datagrid needed. Since the creation of the object is taking a lot of time, it takes some time to show the window. So I decided to move the creation of the object into a background thread and gave the UI thread "back rep" by binding a dispatcher to bind. But it fails
The strange thing is that if I try to set the visibility of a rectangle within the DispatcherWhok, which works but the datagrid does not! any idea? I've tried the same thing with background workers and ThreadStart, but I'm getting the same error. I'm not able to access the Data Grid Object, it is still going inside the Dispatcher, inviting the delegate. There is a lot of gratitude for any suggestion that I am missing something in my understanding of how it works. Thanks!
startupdevelopets = new startupdelegate (create modal); S.BeginInvoke (Representative (IAsyncResult aysncResult) {s.EndInvoke (aysncResult);}, tap); Internal MacDel () {model D = new model (); Dispatcher.Invoke (DispatcherPriority.Normal, (Representative (model d1) of the new action {mModel = d1; // mModel is defined in a property window binding = new binding (); b. Source = mModel; MainDataGrid.SetBinding (TreeView.ItemsSourceProperty, mainb); // with & lt; here dies - because a separate thread it owns the owner calling thread can not use this object}}
Update <. / Strong>: ... a dispatchertimer that will be able to run just once get binding code in its tick representative But I'm still curious why does not the code above
I have another way
Binding should not be called by the code, instead you should define it in XAML.
You add a more DependencyProperty to the type model on your window and call it " CurrentModel "and can set your initial value to zero. Area only to find a property called mModel that DependencyProperty?
You can define the current grid of the current grid or the control in which XAML.
And in its end of the representative, Dispatcher.Invoke should set the current MODEL only, the binding will be done automatically.
Comments
Post a Comment