Problem - user clicks "do something" button (view), view model receives command and passes it to model (function call). Some time passes and model is done processing data (async). How does model notifies viewmodel about "need update"/"done"? What is the best aproach? How can i seperate Model from ViewModel in this scenario?
I think the normal approach for doing this is to use the INotifyPropertyChanged interface. I'm not 100% certain how it works as I'm still fairly new to WPF, but normally you fire an event whenever a property changed, passing in the name of the property and that updates the binding for that property.
Below's some sample code. You'd then be binding to the IsSelected property (as I believe this should be your ViewModel).
public class TestProperty : INotifyPropertyChanged
{
public Boolean IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
this.NotifyPropertyChanged("IsSelected");
}
}
private bool isSelected;
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Notifies the property changed.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
private void NotifyPropertyChanged(String propertyName)
{
this.VerifyPropertyName(propertyName);
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
You could implement a plain old event in your Model which can be subscribed to from the ViewModel.
Update
In response to your comment.
If you are using multiple threads, then you will need to know about the "Dispatcher" framework to ensure that calls from non-UI threads are properly synchronized onto the UI thread. This is a requirement of WPF. Please see:
You might find the sample applications of the WPF Application Framework (WAF) helpful. They show how the model can communicate with the ViewModel or the View via events.