tags:

views:

65

answers:

3

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?

+1  A: 

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));
            }
        }
}
Ian
+4  A: 

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:

http://msdn.microsoft.com/en-us/magazine/cc163328.aspx

chibacity
How do i do this? :) New school here!
0xDEAD BEEF
@0xDEAD BEEF As well as subscribing to events from components you have not written yourself, it is very useful to know how to implement your own events e.g. http://msdn.microsoft.com/en-us/library/w369ty8x.aspx. Events can be a difficult concept for beginners though - my apologies in advance :)
chibacity
@@0xDEAD BEEF Not sure if that is the best example of implementing events, but if you have a C# book, then it should cover this topic.
chibacity
Jap, but BeginInvoke needs control (ModelView), but Model is not aware of any user controls. Am i right?
0xDEAD BEEF
PS - talking about multithreading now..
0xDEAD BEEF
@0xDEAD BEEF Please see my update.
chibacity
Oh Right! Dispatcher was what i am looking for! Thank you and close! :)
0xDEAD BEEF
A: 

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.

jbe