l33t
l33t

Reputation: 20016

Asynchronous update using MVVM?

My model contains a large data object that I update from code-behind (currently this happens on the UI thread but I intend to do this in a worker later on). One of my windows has a viewmodel for this model and outputs this data object using an IValueConverter. The problem is that this conversion takes several seconds. Hence, I need my window to fetch this updated data asynchronously. How can this be done?

MyControl.xaml:

<UserControl.DataContext>
   <local:DataViewModel x:Name="dataViewModel"/>
</UserControl.DataContext>

Then in code:

// This triggers my value converter and blocks the UI for several seconds!
dataViewModel.HeavyObject = data;

Where should the multi-threading code go? Should it be part of the viewmodel or my value converter?

Upvotes: 1

Views: 287

Answers (2)

blindmeis
blindmeis

Reputation: 22445

look at IsAsync="True" for your binding and also check PriorityBinding.

here you find more about PriorityBinding

Upvotes: 1

Big Daddy
Big Daddy

Reputation: 5234

Given a choice between your view-model and value converter, I'd vote for the view-model. However, I'd much rather see your long-running operations in a repository or client-side service that are responsible for interacting with your data store.

Upvotes: 1

Related Questions