Reputation: 300
Currently I'm learning C# with WPF. My mainapproach is to use the MVVM pattern the best I can but now I'm a bit confused.
In my Application for all my views I have a viewmodel:
private DruckviewViewModel ViewModel { get; set; }
public Druckview()
{
ViewModel = new DruckviewViewModel();
DataContext = ViewModel;
InitializeComponent();
}
Is this the suggested way to implement the ViewModel into the View or are there better ways to do it?
Upvotes: 2
Views: 945
Reputation: 32445
MVVM doesn't mean no code-behind.
MVVM is the pattern of separation of concerns. It helps to separate your application's architecture to the three parts(in order of appearance):
Model
View
ViewModel
Where Model
is class containing your business logic.
View
represents your view class which contains only view related logic(XAML
and code-behind
) It is Ok to have code-behind unless code contains only view's logic (for example in button click eventhandler you copy color of one textbox to another, which of course can be done in XAML
, but from MVVM
point of view it is not important)
ViewModel
represents View's behavior without any reference to the View
.
Notice that for example this property on my opinion will violate MVVM
pattern, because Visibility
is view related type
public Visibility MyVisibility { get; set; }
So dependencies between parts of MVVM
goes like this:
Model
doesn't know about anything
ViewModel
know only about Model
View
know about ViewModel
View ---> ViewModel ---> Model
I think for using MVVM
is not important how tightly View
bounded to the ViewModel
. It is already bounded, because you use ViewModel
's properties and commands.
Not bounding tightly (for example using interface as ViewModel
) will give your possibility to test View without real ViewModel
by creating own "design-time" viewmodels for example.
If your current solution works and satisfy your needs and you just starting with MVVM
and WPF
then continue with that until you meet need to fully isolate View
from ViewModel
's types
Upvotes: 2
Reputation: 16991
This will work, but it isn't really true to the MVVM pattern, as the View is now directly tied to the View Model.
Most existing MVVM frameworks use the concept of a View Manager. A class that creates a view from a view model instance, connects them together, and displays the view. You would end up with something like this:
DruckviewViewModel vm = new DruckviewViewModel()
ViewManager.Instance.DisplayViewFor(vm);
It would figure out, based on naming conventions, that DruckviewViewModel
uses the Druckview
. It would create the view, set the DataContextProperty, and make the view visible.
Without using one of these frameworks, this is a lot of work to build on your own, but this is considered a "Best Practice" pattern.
You may want to consider using an existing framework, a good list comparing their features can be found here.
BTW, if you are wondering how to get intellisense in the XAML designer without setting the DataContext in the constructor of the view. The proper way to do it is to add a design instance in XAML, with an attribute like this.
d:DataContext="{d:DesignInstance local:DruckviewViewModel}"
Upvotes: 1