MVVM - controls versus views

I am making a prototype in WPF without using MVVM. Now it has got such a size that I will reorganize it to use MVVM.

When I started the project, I jumped straight and created UserControls for many things.

Now I break things down in Views and ViewModels. But, I end up Views that contain UserControls; UserControls have bindings to the objects that are now in the Model.

So, is the concept of UserControls dead in MVVM? I mean, in the past (WinForms, ASP.NET, etc.) you would have a project called "Controls" and reuse. Is the View in MVVM a direct replacement for a typical UserControl?

+4
source share
3 answers

A UserControl in WPF is a bit more than a ContentControl with a few changed default property values. A ContentControl is nothing more than a piece of content that may have a template used to determine its appearance.

The MVVM method skips the broker and defines the views as DataTemplate s. Then you only need to bind the virtual machine to the WPF visual tree somewhere, and WPF will display it using the DataTemplate . For instance:

 <ContentControl Content="{Binding SomeViewModel}"/> <ItemsControl ItemsSource="{Binding SomeViewModels}"/> 
+6
source

The way I see UserControls in the MVVM world is the view. Instead of thinking of your WPF form as a single view, you can think of it as a collection of one or more views. In this way, UserControl can encapsulate a standard reusable view that can be included in several composite views. Great for reuse, and yet they are still validated.

+2
source

Mmmm ... when I had user controls, I just passed the DataContext from the view to the user control (the subinfection needed by this user control). But it’s true that it’s sometimes difficult to map the ViewModel to the views, UserControls, ...

+1
source

Source: https://habr.com/ru/post/1286662/


All Articles