Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista Tutorial - MVVM-implementation

 
 
Old 05-06-2008   #1 (permalink)
Maximilian
Guest


 
 

MVVM-implementation

I have tried following John Gossman's ContactDatabase-example to make a
simple application to divide a two decimals, just to grasp the concept of the
MVVM-design-pattern. I would like some feeback on it ...

First I have a supersimple Model like this:

public class Model
{
public decimal A;
public decimal B;
}

My ViewModel is a bit more complicated:

public class ViewModel : INotifyPropertyChanged
{
Model model;
decimal result;
ICommand divide;

public ViewModel(Model model)
{
this.model = model;
}

public decimal A
{
get { return model.A; }
set { model.A = value; OnPropertyChanged("A"); }
}

public decimal B
{
get { return model.B; }
set { model.B = value; OnPropertyChanged("B"); }
}

public decimal Result
{
get { return result; }
internal set { result = value; OnPropertyChanged("Result"); }
}

public ICommand Divide
{
get
{
if (divide == null)
divide = new DivideCommand(this);
return divide;
}
}

public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName) { ... }
}

public class DivideCommand : ICommand
{
ViewModel viewModel;

public DivideCommand(ViewModel viewModel)
{
this.viewModel = viewModel;
this.viewModel.PropertyChanged += new
PropertyChangedEventHandler(viewModel_PropertyChanged);
}

void viewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "B")
OnCanExecuteChanged();
}

public bool CanExecute(object parameter)
{
return viewModel.B != 0;
}

public event EventHandler CanExecuteChanged;
void OnCanExecuteChanged() { ... }

public void Execute(object parameter)
{
viewModel.Result = viewModel.A / viewModel.B;
}
}

And lastly I have my View:

<Grid x:Name="View" DataContext="..."> // Im setting the DC to a new
ViewModel in App.xaml.cs
<Button Command="{Binding Path=Divide}">Divide</Button>
<TextBox Text="{Binding Path=Result, Mode=OneWay}" />
<Slider Value="{Binding Path=A}" />
<Slider Value="{Binding Path=B}" />
<Label Content="{Binding Path=A, Mode=OneWay}" />
<Label Content="{Binding Path=B, Mode=OneWay}" />
</Grid>

How does this look? Should I have the method to Divide in the model or in
the viewmodel? Any other comments... ?

Thanks in advance!

/Maximilian

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
MOM.Implementation file Vista performance & maintenance
AHCI Implementation Vista General
MOM.Implementation Error Vista performance & maintenance
EFI Implementation on Vista Vista installation & setup
UAC - practical implementation? Vista security


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46