日期选取器Date picker

日期选取器向你提供了一种标准化方式,可使用户通过触摸、鼠标或键盘输入选取本地化格式的日期值。The date picker gives you a standardized way to let users pick a localized date value using touch, mouse, or keyboard input.

日期选取器示例

获取 Windows UI 库Get the Windows UI Library

WinUI 徽标

Windows UI 库 2.2 或更高版本包含此控件的使用圆角的新模板。Windows UI Library 2.2 or later includes a new template for this control that uses rounded corners. 有关详细信息,请参阅圆角半径For more info, see Corner radius. WinUI 是一种 NuGet 包,其中包含用于 Windows 应用的新控件和 UI 功能。WinUI is a NuGet package that contains new controls and UI features for Windows apps. 有关详细信息(包括安装说明),请参阅 Windows UI 库For more info, including installation instructions, see Windows UI Library.

平台 API: DatePicker class(DatePicker 类),Date 属性Platform APIs: DatePicker class, Date property

这是正确的控件吗?Is this the right control?

使用日期选取器以使用户选取日历上下文不重要的已知日期,例如生日。Use a date picker to let a user pick a known date, such as a date of birth, where the context of the calendar is not important.

有关选择正确日期控件的详细信息,请参阅日期和时间控件文章。For more info about choosing the right date control, see the Date and time controls article.

示例Examples

XAML 控件库XAML Controls Gallery
XAML controls gallery

如果已安装 XAML 控件库应用,请单击此处打开此应用,了解 DatePicker 的实际应用If you have the XAML Controls Gallery app installed, click here to open the app and see the DatePicker in action.

入口点显示选定的日期,当用户选择该入口点时,会从中间垂直展开一个选取器图面以供用户进行选择。The entry point displays the chosen date, and when the user selects the entry point, a picker surface expands vertically from the middle for the user to make a selection. 日期选取器会覆盖其他 UI;它不会将其他 UI 推开。The date picker overlays other UI; it doesn't push other UI out of the way.

日期选取器展开示例

创建日期选取器Create a date picker

本示例演示如何创建附带标头的简单日期选取器。This example shows how to create a simple date picker with a header.

<DatePicker x:Name="birthDatePicker" Header="Date of birth"/>
DatePicker birthDatePicker = new DatePicker();
birthDatePicker.Header = "Date of birth";

生成的日期选取器如下所示:The resulting date picker looks like this:

日期选取器示例

注意:有关日期值的重要信息,请参阅日期和时间控件文章中的 DateTime 和 Calendar 值Note  For important info about date values, see DateTime and Calendar values in the Date and time controls article.

获取示例代码Get the sample code