I have a xaml window that I want to bind the Escape key
to different commands in my view model depending on the value of a boolean property.
IsSearching == true
, bind to a CancelSearch command
IsSearching == false
, bind to a Close command.
Is there a way to do this without a code behind?
<Window>
<Window.InputBindings>
<KeyBinding Command="{Binding SomeCommand}" Key="F5"/>
</Window.InputBindings>
</Window>
This is a workaround
private ICommand someCommand;
public ICommand SomeCommand
{
get
{
return someCommand
?? (someCommand = new ActionCommand(() =>
{
if(IsSearching)
OnCancelExecute();
else
OnCloseExecute();
}));
}
}