I'm trying to listen to Enter key in calendar control via MVVM. I am able to listen to any other key but not Enter for some reason. Using code behind is not a solution as I want to do command binding with the ViewModel… so please handling PreviewKeyDown will not do.
This is in the view:
<Calendar SelectionMode="SingleRange">
<Calendar.InputBindings>
<KeyBinding Key="Return"
Command="{Binding CloseCalendarCommand}" />
<KeyBinding Key="Esc"
Command="{Binding CloseCalendarCommand}" />
</Calendar.InputBindings>
</Calendar>
The Escape key is working fine and triggers CloseCalendarCommand. Enter key does not trigger the command. Before MVVM I used PreviewKeyDown="FilterCalendar_PreviewKeyDown" to handle the Enter Key...
private void FilterCalendar_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
e.Handled = true;
CalendarPopup.IsPopupOpen = false;
}
}
but this is goes against MVVM concept and I want to do other things in the viewModel once the calendar popup is closed that I cannot from the code behind.
In the viewmodel I'm using Prism Commands:
CloseCalendarCommand = new DelegateCommand(OnCloseCalendarCommand);
private void OnCloseCalendarCommand()
{
IsCalendarVisible = false;
...
}
public ICommand CloseCalendarCommand { get; }