-
Dec 2nd, 2010, 02:59 PM
#1
Thread Starter
Frenzied Member
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?
-
Dec 2nd, 2010, 05:03 PM
#2
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...
-
Dec 3rd, 2010, 02:07 AM
#3
Thread Starter
Frenzied Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|