Search code examples
c#wpfmvvmmvvm-lightrouted-commands

RelayCommand vs EventToCommand


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!


Solution

  • 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.