Results 1 to 3 of 3

Thread: MVVM update view from model

  1. #1

    Thread Starter
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    MVVM update view from model

    At the moment I am teaching my self WPF, with the MVVM model. And I'm getting it .
    But one thing I don't understand. If there is a simple view:
    Code:
    				<ProgressBar
    				Height="18"
    				Canvas.Top="89"
    				Canvas.Left="14"
    				Width="621" 
    			Value="{Binding ProgressPercentage, Mode=TwoWay}"/>
    And I have ViewModel that implements the INotifyPropertyChanged
    Code:
    public class ViewModelClass: INotifyPropertyChanged
    {
    private ModelClass mc;
    public int ProgressPercentage 
    {
    	get { return this.mc.ProgressPercentage;}
    	set 
    	{
    		if (this.mc.ProgressPercentage != value)
    		{
    			this.zah.ProgressPercentage = value;
    			InvokePropertyChanged ("ProgressPercentage");
    		}
    	}
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void InvokePropertyChanged(string propertyName)
     {
    	 var e = new PropertyChangedEventArgs(propertyName);
    	 PropertyChangedEventHandler changed = PropertyChanged;
    	 if (changed != null) changed(this, e);
     }
    }
    And there is a simple model:
    Code:
    public class ModelClass 
    {
    public int ProgressPercentage { get;set;}
    public void DoSomething()
    {
        this.ProgressPercentage ++;
    }
    }
    The model update it's %Progress, but the ViewModel and the View don't know.
    If the update takes place in the ViewModel it works, because the INotifyPropertyChanged. And the Model shouldn't implement that interface (I think, because it is UI-stuff that shouldn't be in the model).
    Can anyone explain how this should work?
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: MVVM update view from model

    Firstly, there is no law against the Model implementing INotifyPropertyChanged and the View binding to the model directly. When you have something as simple as that model (I assume this is a slightly contrived example, but still) there is very little harm in combining the Model and ViewModel.

    In a more complicated example, you would use all the normal techniques to update the ViewModel from the Model: have the model call the ViewModel directly, have the Model raise an event that the ViewModel is subscribed to, etc...

  3. #3

    Thread Starter
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: MVVM update view from model

    This is very simple example (still learning). I want to know how it should be done "full MVVM". A work-around can be found easy
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width