Keyboardfocus on calendar control

Posted by: lutz.boscher on 13 February 2019, 6:46 pm EST

    • Post Options:
    • Link

    Posted 13 February 2019, 6:46 pm EST

    Hi,

    At startup I want to set the focus to the calendar control in code behind and after that, the user should navigate through the calendar with the keyboard only.

    I have attached a small sample. After clicking the button, the keyboardfocus is set to the calendar, but the calendar does not response to the arrow-keys. First, I have to click in the calendar control to get the keyboardnavigation working.

    What can we do to navigate inside the calendar without using the mouse?

    Lutz Boscher

    Regards

    Lutz Boscher

  • Posted 13 February 2019, 6:50 pm EST

    The attachemnt was not sent, vwnt thoought it was droped-

    the two files as plain text:

    MainWindowsxaml





    <c1:C1Calendar x:Name=“_cal” Focusable=“True” GotKeyboardFocus=“C1Calendar_GotKeyboardFocus”

    LostKeyboardFocus=“C1Calendar_LostKeyboardFocus” Canvas.Left=“280” Canvas.Top=“120” />





    MainWindow.xaml.cs:

    using System;

    using System.Collections.Generic;

    using System.Diagnostics;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Imaging;

    using System.Windows.Navigation;

    using System.Windows.Shapes;

    using System.Windows.Threading;

    using C1Calendar;

    namespace C1Calendar {

    ///



    /// Interaktionslogik für MainWindow.xaml

    ///


    public partial class MainWindow : Window {

    public MainWindow() {

    InitializeComponent();

    }

    	private void C1Calendar_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
    		Debug.WriteLine("In GotKeyboardFocus");
    		_cal.SelectedDate = DateTime.Today;			
    	}
    
    	private void C1Calendar_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
    		Debug.WriteLine("In LostKeyboardFocus");
    	}
    
    	private void Button_Click(object sender, RoutedEventArgs e) {
    		
    		var sucess = _cal.Focus();
    		var focusedElement = Keyboard.Focus(_cal);
    		Debug.WriteLine($"Setting KeyboardFocus to _calendar: {sucess}. Focus has {focusedElement}");
    	}
    
    	private void Button_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
    		Debug.WriteLine("In Button_LostFocus");
    	}
    }
    

    }

  • Posted 14 February 2019, 8:55 pm EST

    Hello Lutz,

    For this you need to access the C1CalendarItem present inside C1Calendar and set keyboard focus to it. You may use C1Calendar’s PreviewKeyDown event for this```

    private void Window_Loaded(object sender, RoutedEventArgs e)

    {

    calItem = FindVisualChildren<C1.WPF.Schedule.C1CalendarItem>(_cal).ToList()[0];

    calItem.Focusable = true;

    _cal.Focus();

    }

    private void _cal_PreviewKeyDown(object sender, KeyEventArgs e)
    {
            if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down)
            {
                UIElement keyboardFocus = calItem as UIElement;
                TraversalRequest tRequest = new TraversalRequest(FocusNavigationDirection.Next);
                if (keyboardFocus != null)
                {
                    keyboardFocus.MoveFocus(tRequest);
                }
           }
    }
    
    ```
    
    Regards,
    Ruchir
    [zip filename="Focus_Calendar.zip"]https://gccontent.blob.core.windows.net/forum-uploads/file-fb1946ee-1402-4d51-a8b6-ca881bb4f8d1.zip[/zip]
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels