Reputation: 413
I'm little confused about RelayCommand
and EventToCommand
in Mvvmlight.
It seems that EventToCommand
handle the EventTrigger
and call a RelayCommand
to do job.
Such as:
<i:Interaction.Triggers>
<i:EventTrigger x:Uid="i:EventTrigger_1" EventName="MouseLeftButtonUp">
<cmd:EventToCommand x:Uid="cmd:EventToCommand_1" Command="{Binding Form_MouseLeftButtonUpCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Is my understanding correct?
So, can we use RelayCommand
directly with EventTrigger
, no need to use EventToCommand
?
Thanks for your help!
Upvotes: 2
Views: 2475
Reputation: 413
Thank Tilak for your useful answer.
With the explanation from Tilak, I did mess up like putting a binding in a event handler (such as Button GotFocus="{Binding DoJob}") --> Build failed and found that Event Handler like this does not support Binding. We can only bind in Command (such as Button Command="{Binding DoJob}" /> and default event is invoked in this situation, with button, it should be Click event). Do something stupid will help me to understand the life more - LOL
can we use RelayCommand directly with EventTrigger, no need to use EventToCommand?
Actually, I intend NOT to use EventToCommand, and I found the solution for that: use InvokeCommandAction instead (belongs to System.Windows.Interactivity - Mvvm-light also refers to this assembly).
Upvotes: 1
Reputation: 30718
EventToCommand
is a custom behavior. It is first provided by Expression blend team and now Part of WPF 4. If you're not using WPF4. you require Blend SDK from here.
Behaviors encapsulates functionality as reusable components. These are to be used when feature is not present by default. For example Adding Command support to Label, Combobox etc.
can we use RelayCommand directly with EventTrigger, no need to use EventToCommand?
No. RelayCommand
is a shortcut to avoid code redudnency to define custom commands.It extends ICommand
whose delegates can be attached for Execute(T)
and CanExecute(T)
. It is similar to DelegateCommand
of Prism library.
<cmd:EventToCommand x:Uid="cmd:EventToCommand_1" Command="{Binding Form_MouseLeftButtonUpCommand}" PassEventArgsToCommand="True"/>
In above line cmd:EventToCommand
is additional feature to the underlying control. Form_MouseLeftButtonUpCommand
is the Command
it executes. This command can be encapsulated as RelayCommand
.
Upvotes: 4