MVVM - view logic: view vs viewmodel

Where should logic usually look? In the view (including the code behind) or in the viewmodel?

According to the logic I understand everything that is used to change the presentation (making it dynamic), changing the properties of elements: Visibility, IsEnabled, Contentetc. based on some conditions.

I am trying to select the correct operator:

  • ViewModel is responsible for all the properties of the view, if the view requires some logic - this should be the task of the viewmodel.

  • View is a view in the form of a viewmodel, the viewmodel requires only the minimum for the model to be exposed, so the logic should be part of the view.

Logic in the view.

Example to display some text:

<Grid Visibility="{Binding TextAvailable, Converter=...}">
    <TextBlock Text="{Binding Text}" Visibility="{Binding TextOk, Converter=...}" />
</Grid>

xaml, , viewmodel 2 : TextAvailable TextOk, Text.

. , : . , , : .

.

Xaml :

<TextBlock Text="{Binding Text}" Visibility="{Binding ShowText, Converter=...}" />

:

public bool ShowText => TextAvailable && TextOk;

, / ( , ), OnPropertyChanged(nameof(ShowText)), - . , /.


viewmodel (xaml), . , ( ).

, , , , , . MVVM? ?

+4
2

, , , . , .

, MVVM ViewModel , . , . ViewModel , : , , , .

, , , ViewModel. ; , ViewModel, , , . , , ViewModel, , , XAML . , XAML, .

ViewModel View: , , View, , . , MVVM, , , , ViewModel View , View .

+2

-

2.View - viewmodel, viewmodel , .

.

, " ", - (, , - , VM) .

" UIElementA DataContext UIElementB, UIElementA View ViewModel?" , UIElementA UIElementB VM, UIElementB .
( ), DataContext, (OP ).

:
UI ViewModel , .

0

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


All Articles