Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Store Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems.

Go Back   Vista Forums > Vista technology newsgroups > Avalon

MVVM-implementation

Reply
 
Thread Tools Display Modes
Old 05-06-2008   #1 (permalink)
Maximilian
Guest
 
Posts: n/a

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
  Reply With Quote

Reply

Thread Tools
Display Modes









Vistax64.com 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 2005-2008

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 47 48