xamlwinui-3winuiwindows-app-sdkwinui-xaml

How to bring the selected date into view in the calendarview control of WinUI?


I have created a calenderview in winui3 for my project. I am setting the selected date in the code behind file. But the calendar is not bringing the selected date into view. How to fix this? enter image description here

If I set the selected date which is not present in this view in the code behind file like below

MyCalendar.SelectedDates.Add(someDate);

After adding the selectedate it is not brought into the view. Should we do it explicitly ?


Solution

  • You can use SelectedDatesChanged and SetDisplayDate for this:

    MyCalendar.SelectedDatesChanged += MyCalendar_SelectedDatesChanged;
    
    private void MyCalendar_SelectedDatesChanged(CalendarView sender, CalendarViewSelectedDatesChangedEventArgs args)
    {
        if (args.AddedDates.Count < 1)
        {
            return;
        }
    
        sender.SetDisplayDate(args.AddedDates[0]);
    }