Alternative MVVM ICommand

I started creating a wpf mvvm application. It seems that a bunch of ICommands seems to be a vital component of the ViewModel, in order to have a loosely coupled way to allow the view to interact with the viewmodel.

My question is, why can't I bind a method directly?

I used the implementation of ICommand Josh Smith RelayCommand, which allows you to enter delgates in the ICommand object, but is there really an easier way to allow button clicks to invoke a method in the viewmodel?

I am new to MVVM, I believe that I need some enlightenment

+3
source share
4 answers

, Button () , . Command ICommand. A RelayCommand (aka DelegateCommand) - ICommand, .

, , :

<Button Command="{ViewModelMethod SomeMethodName}"/>

. ICommand, ( ) .

+7

.

: , .

. {Binding MyProperty} , {ViewMethod MyMethod} ?

, "" , . , # - .

MarkupExtension Binding, . . , , MethodCall CodePlex: http://methodcallthing.codeplex.com/

, 'this' , . , . - , (OneWayToSource).

+6

ICommand CanExecute, . . ICommand - .

+5

, Microsoft , - , , , CanExecute . , CanExecute DependencyProperty, viewmodel, , ?

Perhaps they also thought that it was necessary to separate the implementation of the command from the datacontext control. But again, this seems unnecessary to me, because the logic should live near the data in question, and is also the main principle of OO.

Personally, I avoid using commands in MVVM because of the clutter you have to create to implement them. I just allow the delegate events with the code view to the presentation model.

+3
source

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


All Articles