Cartesius00
Cartesius00

Reputation: 24414

ViewModels and UI

from the design point of view, I have a complex GUI, what is the recommended granularity for cutting the GUI (Window) into ViewModels, UserControls and DataTemplates. Let's assume I have a window showing list of entities in DataGrid, then some filters in ComboBoxes, some detail...

How many ViewModels should I create? ...and DataTemplates and UserControls.

Thank you very much for a good WPF/MVVM design pattern idea.

Upvotes: 0

Views: 318

Answers (2)

Damascus
Damascus

Reputation: 6651

Although your question is not very clear, I'll try to answer it. The number of ViewModels is... just up to you.

My personal approach is to separate my UI into many different custom UserControls, with one ViewModel per control. It allows me to have a crystal-clear architecture with a lot of smart controls designed for one task each. As an example, for my manager application, I have: - One UserControl designed to list all the users - Another one designed to create or edit a user - and one more designed to monitor user's activity.

Separating your application into different task-driven UserControls will help you having:

  • A more easily understandable architecture,
  • A more compact XAML, easier to enhance
  • A chance not to mess with too many Bindings flying everywhere.

About DataTemplates, again, I usually separate the general templates into different ResourceDictionaries.

Anyway, there is no "right way" neither "wrong way" to do, I think there is just "your way". For me, the more granular my code is, the more understandable it will be, assuming that the components are not small enough to be useless. One separated component should be designed for one main goal. Once you've done this separation, MVVM will help you a lot!

Upvotes: 3

ColinE
ColinE

Reputation: 70160

Your question is a bit generic, and the real answer will probably depend on your application. A View-Model is a model-of-a-view, so I would expect that you will have one for each of your Views.

I would start simple (KISS) and have in your case a single view-model and view, with the UI controls (DataGrid etc..) placed directly in your view.

If you can, use DataTemplates from day-one, they help to keep top-level layout, separate from your more detailed layout of model objects.

If / when you get to the point where you need to re-use controls / layout / logic elsewhere, then you can start to break up this single view / view-model into a view composed or sub-views or UserControls. You should probably mirror this composition by breaking up your view-model in a similar fashion.

Upvotes: 1

Related Questions