I'm using the MVVM Community toolkit (v 8.2) in a learning project that implements the Risk board game.
I have a working command for some custom buttons, so I know the basics are functioning. But when I add commands bound to Keyboard input, they never fire (breakpoints are never reached), and I can't tell why.
I worried about Focus for a while, but the problem persists even when Window.Focusable=True explicitly, or when the InputBindings are on child elements like a background image or custom buttons (mentioned previously).
I've found many helpful answers to closely related questions here and elsewhere online, but nothing has resolved my specific issue.
MainWindow View XAML:
<Window x:Class="RiskMVVM2.MainWindow"
...
Width="1820" Height="1062"
ResizeMode="NoResize" Name="GameWindow"
WindowStartupLocation="CenterScreen" WindowStyle="None"
FontFamily="Courier New" FontSize="12" >
<Window.InputBindings>
<KeyBinding Key="Return" Command="{Binding ConfirmInputCommand}" />
<KeyBinding Gesture="Shift+Return" Command="{Binding SkipPlayerTurnCommand}" />
</Window.InputBindings>
MainWindow Constructor (View code-behind):
public MainWindow(RiskBoardVM VM, List<Brush> playerColors)
{
this.DataContext = VM;
...
InitializeComponent();
...
}
Relevant ViewModel Code:
...
private bool CanConfirmInput()
{
return true;
}
[RelayCommand(CanExecute = nameof(CanConfirmInput))]
private void ConfirmInput()
{
CurrentGame.State.IncrementPhase();
}
private bool CanSkipPlayerTurn()
{
if ((int)CurrentPhase > (int)GamePhase.Place) return true;
else return false;
}
[RelayCommand(CanExecute = nameof(CanSkipPlayerTurn))]
private void SkipPlayerTurn()
{
CurrentGame.State.IncrementPlayerTurn();
}
...