1

I'm using WinUI3/Windows App SDK 1.3 - XAML/C# My problem is that most UIs today have a de facto "Today" button on their calendar controls. In WASDK/WinUI, the control is called "CalendarView" and works great. Unfortunately, it does not have a "Today" button that when clicked, should scroll the calendar to today's date.

I already have a simple button atop the CalendarView, but looking into the documentations from Microsoft, there's no functionality for what I'm trying to achieve.

Here's the default CalendarView control: Exhibit A

Now here's the calendar with my button: Exhibit B

What I would like to do is when I click on the "Today" button, the calendar will automatically update itself to scroll into today's date. Example, the calendar is currently displaying the month of December 2050, when I click on today button, the calendar should auto-scroll back to the current month, day and year.

Any guidance or tips would be greatly appreciated. Thanks in advance!

2 Answers2

2

In the C# code, create a function to retrieve the current date and scroll the calendar view to today's date.

private void TodayButton_Click(object sender, RoutedEventArgs e)
{
  // Get the current date
  DateTime today = DateTime.Today;

  // Scroll the CalendarView to today's date
  calendarView.ChangeView(today, null, null);
}

In the XAML code, link the "Today" button to this function.

<Button x:Name="TodayButton" Content="Today" Click="TodayButton_Click" />
  • Hi, the ChangeView method is nowhere to be found in https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.calendarview?view=windows-app-sdk-1.2, however, I found this method https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.calendarview.setdisplaydate?view=windows-app-sdk-1.2#microsoft-ui-xaml-controls-calendarview-setdisplaydate(windows-foundation-datetime) which does the job. I appreciate your help. – user21311740 Jun 06 '23 at 02:48
0

https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.calendarview.setdisplaydate?view=windows-app-sdk-1.2#microsoft-ui-xaml-controls-calendarview-setdisplaydate(windows-foundation-datetime)

CalendarView.SetDisplayDate(DateTimeOffset.Now);
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • Your answer could be improved by providing an example of the solution and how it solves the problem. – Tyler2P Jun 06 '23 at 08:41