0

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; }
  • 1
    Add some code here(XAML,ViewModel etc...) like what have you tried and whats not working. – Abin Oct 25 '18 at 14:08
  • OK added some code to explain further. – user3572897 Oct 26 '18 at 15:34
  • `IsCalendarVisible ` property implimenting `NotifyPropertyChanged` Interface ? – Abin Oct 26 '18 at 15:48
  • have you kept a breakpoint on the `CloseCalendarCommand get` and does it hit while you start the application ? If not you have to check your `DataContext`. Check for any output errors too. – Abin Oct 26 '18 at 15:52
  • IsCalendarVisible is obviously INPC. As stated above I'm doing MVVM. I set a breakpoint in OnCloseCalendarCommand and always gets triggered with any key I set in InputBindings except for Enter Key (even if I set modifiers with Enter like Ctrl + Enter etc.) Calendar control must be listening to Enter key itself and not bubbling it up the visual tree. – user3572897 Oct 27 '18 at 20:24
  • Using code behind doesn’t necessarily violate MVVM, which is about separation of concerns, not separation of languages. As long as the code behind is doing purely view stuff, there is nothing wrong. Writing an event handler that simply invoked some view model command or method doesn’t violate MVVM. – Dave M Oct 27 '18 at 20:40
  • However, can solve this in pure xaml like this: https://stackoverflow.com/questions/4897775/wpf-binding-ui-events-to-commands-in-viewmodel – Dave M Oct 27 '18 at 20:41
  • Thank you but no thank you. I'm already using interactions and blend behaviours to bind to SelectedDates but my question was about keyboard input not selection. – user3572897 Oct 29 '18 at 00:19

0 Answers0