Keyboardfocus on calendar control

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

  • 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


  • Replied 13 February 2019, 6:50 pm EST

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

    the two files as plain text:
    MainWindowsxaml
    <Window x:Class="C1Calendar.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:C1Calendar"
    FocusManager.FocusedElement="{Binding ElementName=_cal}"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" >
    <Canvas>
    <c1:C1Calendar x:Name="_cal" Focusable="True" GotKeyboardFocus="C1Calendar_GotKeyboardFocus"
    LostKeyboardFocus="C1Calendar_LostKeyboardFocus" Canvas.Left="280" Canvas.Top="120" />
    <Button Content="Set Focus" Click="Button_Click" Canvas.Left="343" Canvas.Top="332" LostKeyboardFocus="Button_LostKeyboardFocus"/>
    </Canvas>
    </Window>


    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 {
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    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");
    }
    }
    }

  • Marked as Answer

    Replied 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
    Focus_Calendar.zip
Need extra support?

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

Learn More

Forum Channels