patterncsharpMinor
MVVM: Am I doing it right?
Viewed 0 times
rightdoingmvvm
Problem
I read a lot of articles recently concerning XAML, WPF, data binding and MVVM. I also watched some MVVM tutorials and downloaded some MVVM samples. I finally got my first MVVM sample to work, but there seem to be different "flavors" of MVVM, or maybe there are just many people who get the whole thing totally wrong, I don't know. I tried to do it my way and think it's quite a clean and neat approach. But I've never seen any "real-life" code, which I could compare my code with.
What I try to do is edit a model instance (
My
Can you please have a look at my classes and tell me if there's anything I got totally wrong about MVVM? And a suggestion would be nice on how to change the enabled state of the Ok button immediately on any change in the
Model:
View:
```
Nummer:
Name:
Ok
Reset
C
What I try to do is edit a model instance (
Gebiet) with simply 2 properties, 1 string (Name) and 1 integer (Nummer). Plus, I wanted the ViewModel to not modify the Model before the "Ok" button is clicked, so I'm internally working on a copy of the model instance. I don't want to use any 3rd party stuff or MVVM frameworks yet, as I would like to fully get the whole concept first before I start trying to make my life easier. And I was insecure what is the best way to handle the int property, as that is sensitive to invalid user input. The way I did it, the TextBox text simply reverts to its previous value on invalid user input.My
ViewModel is quite big for just 2 props, but that seems to be normal!? My biggest problem was to get the commands into the ViewModel, so I don't need to handle the button clicks in my code behind (aside from closing the window on "Ok").Can you please have a look at my classes and tell me if there's anything I got totally wrong about MVVM? And a suggestion would be nice on how to change the enabled state of the Ok button immediately on any change in the
Name TextBox.Model:
namespace Gebietsmanager
{
public class Gebiet
{
public int Nummer { get; set; }
public string Name { get; set; }
}
}View:
```
Nummer:
Name:
Ok
Reset
C
Solution
Your implementation looks fine to me. In real life application, you will have base implementations for
Also:
INotifyPropertyChanged interface, which will cover some of the most common use cases (firing the event, data validation, commit/rollback scenario). You implement those once, and then simply re-use them in your actual view models.Also:
- You should not use underscores in method names.
- The common implementation of
ICommandinterface usesCommandManagerinternally. Check it out. This way you won't have to manually callRaiseCanExecuteChanged()on your commands.
- You can use
int.TryParsemethod instead oftry catch. Also, there are other ways to deal with data convertion. You can implementIValueConverterinterface and handle invalid input there. You can implement your own control which would filter the user input for you (I actually posted the code here on CodeReview a few years back). More options will be available as you become more comfortable with WPF. But using strings for properties which require conversion is a good way to start.
- You don't have to set
CommandParameterif you don't use it.
Context
StackExchange Code Review Q#124361, answer score: 3
Revisions (0)
No revisions yet.