wpftriggersinteractioncanexecute

C# WPF CanExecute for InvokeCommandAction


Is it possible to set a CanExecute on an InteractionTrigger?

This is my code, but I know in some instances the ButtonCommand will be NULL, So I want to be sure that I do not cause any issues.

<i:Interaction.Triggers>
  <i:KeyTrigger Key="Return">
    <i:InvokeCommandAction Command="{TemplateBinding ButtonCommand}" />
  </i:KeyTrigger>
</i:Interaction.Triggers>

Many thanks


Solution

  • If and when ButtonCommand is null, the Command property of the InvokeCommandAction will also be set to null and since the implementation checks the reference before trying to invoke the command you won't get any NullReferenceException.

    So you don't have to do anything at all. This should work as expected out of the box.