In a WPF Button we have a Command parameter which can be binded to ICommand.
<Button Command="{Binding SomeCommand}"/>
We can also use EventTriggers with InvokeCommandAction to fire a ICommand.
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding SomeCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
What is the difference between them and when to use which?
Update:
I have noticed difference in the following scenario:
- I have a textbox which validates using IValudationRule if the textbox is empty.
- I added MultiDataTrigger condition to have the IsEnabled property of a save button to be set to false when the Validation.HasError equals to true.
Using the Button Command all works good, but using the EventTrigger it doesn`t work.
Any reason for this?
Snippets you provide are almost the same,if you don’t use
CanExecute.InvokeCommandActionis not nativeWPFclass, it is created inInteractionlibrary for the cases when control doesn’t provideCommandand you have to bind Command to some event. for example when you need command onListBox.SelectionChangedor etc.So based on above, my suggestion is, always use Command if it is possible, and use
EventTriggeronly when you can’t go without it.Also Note, than
ICommandalso provideCanExecutebased of which button can enable/disable, which will not work in second case