如何使CalendarView自动滚动到今天?

huangapple go评论92阅读模式
英文:

How do I make CalendarView automatically scroll to today?

问题

我的问题是,今天大多数的用户界面都在他们的日历控件上有一个事实上的“今天”按钮。 在WASDK/WinUI中,控件称为“CalendarView”,它工作得很好。 不幸的是,它没有一个“今天”按钮,当点击时,应该滚动到今天的日期。

我已经在CalendarView的顶部有一个简单的按钮,但是查看了Microsoft的文档后,发现没有实现我想要的功能。

这是默认的CalendarView控件:
示例 A

现在这是带有我的按钮的日历:
示例 B

我想要做的是,当我点击“今天”按钮时,日历会自动更新自己以滚动到今天的日期。 例如,日历当前显示的是2050年12月,当我点击今天按钮时,日历应该自动滚动回到当前的月份、日和年。

非常感谢您提前提供的任何指导或建议!

英文:

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!

答案1

得分: 2

在C#代码中,创建一个函数来获取当前日期并滚动日历视图到今天的日期。

private void TodayButton_Click(object sender, RoutedEventArgs e)
{
  // 获取当前日期
  DateTime today = DateTime.Today;

  // 将CalendarView滚动到今天的日期
  calendarView.ChangeView(today, null, null);
}

在XAML代码中,将“今天”按钮链接到这个函数。

<Button x:Name="TodayButton" Content="今天" Click="TodayButton_Click" />
英文:

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&#39;s date
  calendarView.ChangeView(today, null, null);
}

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

&lt;Button x:Name=&quot;TodayButton&quot; Content=&quot;Today&quot; Click=&quot;TodayButton_Click&quot; /&gt;

答案2

得分: 0

CalendarView.SetDisplayDate(DateTimeOffset.Now);

翻译:

CalendarView.SetDisplayDate(DateTimeOffset.Now);
英文:

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);

huangapple
  • 本文由 发表于 2023年6月4日 22:56:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76400988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定