wpf - What is the preferred way to connect viewmodels to their views? -


In the past, working with MVVM, I have created each view as the DataTemplate visual model I have just started using MVVM Lite to deal with adding them, and saw that they have ViewModelLocator I have noticed that many other toolkits have some variations in it, but its use is What is the benefit of using DataTemplate

? Which is the best practice to add your visual and visual models?

There are two different approaches, and one is not "the right way".

The approach that helps with a ViewModelLocator or similar is a "see-before" approach for the development of MVVM, by that means that you can see your view Start with the designer, and then make the match to match the match. Logically, the scenes often make other views, and the view model usually loads through some type of locator and pops up for a given view. Messaging or services are used to hook up the proper models in newly generated view models.

While working with Blend, in particular, it is advantageous to have a little easier to visually design.

The other approach is "work-before-first" work by that, you generate your view models, and then use the datatapplet to view views. To set up the appropriate model directly, ViewModels will create / compose other VMs. This is generally very clear from the perspective of the programmers (IMO), because things work directly. However, designing and working from designer's perspective is generally more difficult, because design-time data is more difficult to generate, etc.

Both methods are completely valid, and have strong advantages and disadvantages. Different MVMM netwomen have to like an approach from one to the other for different reasons - usually they are coming from the point of view of the code code or the designer point of view.

Comments