Nullable and clearable datetime picker component for Maui
$ dotnet add package Sebarslan.Maui.NullableDateTimePickerThe Nullable DateTimePicker is a custom calendar control for selecting a nullable date and time value in a .NET MAUI application. It provides a consistent and platform-independent user interface for selecting dates, and allows the user to clear the value if needed.
This control uses the <a href="https://github.com/CommunityToolkit/Maui" target="_blank">CommunityToolkit.Maui</a> Popup
To use the Nullable DateTimePicker control in your .NET MAUI application, follow these steps:
0- Add the Sebarslan.Maui.NullableDateTimePicker nuget package to your project and add the .ConfigureNullableDateTimePicker() element to the MauiProgram.cs file in your project.
<pre> <code> public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder() .UseMauiApp<App>() .ConfigureNullableDateTimePicker() .... </code> </pre>1- Add the NullableDateTimePicker control to your XAML layout file:
xmlns:ndtp="clr-namespace:Maui.NullableDateTimePicker;assembly=Maui.NullableDateTimePicker"
<pre> <code> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:ndtp="clr-namespace:Maui.NullableDateTimePicker;assembly=Maui.NullableDateTimePicker" x:Class="Maui.NullableDateTimePicker.Samples.MainPage"> </code> </pre>2- And add the following code to the place where you want to use DateTimePicker and then connect the SelectedDateTime property with the Datetime Property in your ViewModel.
<pre> <code> <ndtp:NullableDateTimePicker SelectedDateTime="{Binding MyDateTime}" Mode="Date" /> </code> </pre>1- Add your entry and button for datetime in your xaml page (eg. MainPage.xaml)
2- Then, when you click on the button or entry, define the options and call NullableDateTimePicker.OpenAsync(options) to open the calendar in your xaml.cs file. (eg. MainPage.xaml.cs)
More examples, please see the samples project
Date and Time Selection
Select both date and time values seamlessly.
Calendar Navigation
Navigate through months and years with previous/next buttons.
Week Numbers
Option to display week numbers in the calendar view.
Day Overflow
Shows days from the previous and next months to fill the calendar grid.
Month Picker
Tap on the month name to open a month selector.
Supports year switching while selecting a month.
Switch Between Date and Time Modes
Easily toggle between date and time pickers within the same interface.
Flexible Usage
Can be used:
Embedded Mode:
Used as an inline input field (as a ContentView).
The date or time picker opens in a modal popup when the icon or input field is tapped.
Popup Mode:
The calendar or time picker can be opened directly as a modal,
without using an input field (for standalone picker usage).
Time Picker Options
Action Buttons
null).Localization Ready
Compatible with various date, time, and number formats based on the user's locale.
Customizable Styles
Fully customizable appearance (colors, fonts, icons) to match your app’s design.
| Option | Description | Default Value |
|---|---|---|
| AutomationId | You can give your own automation id. With this you can access Calendar elements. Example: {Your-AutomationId}_CalendarYearsPicker, {Your-AutomationId}_CalendarOkButton | empty |
| BodyBackgroundColor | Background color of the calendar. | White |
| CancelButtonText | The Cancel button text, optionally supplied via Translations. | Cancel |
| ClearButtonText | The Clear button text, optionally supplied via Translations. | Clear |
| DayNamesStyle | Style of the day names in the calendar. | null |
| DayStyle | Style of the days in the calendar. | null |
| DisabledDayStyle | Style of the disabled days in the calendar. | null |
| ForeColor | It is used for the color of texts that cannot be styled in the calendar. | Dark:White, Light:Black |
| HeaderBackgroundColor | Background color of the calendar's header. | #2b0b98 |
| HeaderForeColor | Gets or sets the foreground color of the control's header. | White |
| Is12HourFormat | Determines whether to display the am/pm picker for the 12-hour format. | false |
| MaxDate | Maximum selectable date of the control. | DateTime.MaxValue |
| MinDate | Minimum selectable date of the control. | DateTime.MinValue |
| Mode | Specifies the picker mode of the control. Valid values are Date, DateTime, and Time. | Date |
| OkButtonText | The OK button text, optionally supplied via Translations. | OK |
| OtherMonthDayStyle | Style of the other month days in the calendar. | null |
| PopupBorderColor | Color of the popup border. Set to transparent or null to hide the border. | Transparent |
| PopupBorderWidth | Thickness of the border around the popup. | 0 |
| PopupCornerRadius | Corner radius of the popup's border for rounded edge appearance. | 0 |
| PopupPadding | Internal spacing between the popup's content and its border. | 0 |
| PopupPageOverlayColor | Background overlay color behind the popup. Useful for dimming the content. | Black, %50 opacity [Colors.Black.WithAlpha(0.5f)] |
| SelectedDateTime | Gets or sets the nullable date and time value of the control. | null |
| SelectedDayStyle | Style of the selected day in the calendar. | null |
| ShowClearButton | Clear button can be hidden/shown. If true, the button is displayed. | true |
| ShowOtherMonthDays | Determines whether to display other month days in the calendar. | true |
| ShowWeekNumbers | Determines whether to display week numbers in the calendar. | false |
| ToolButtonsStyle | Style of the control's tool buttons. | null |
| WeekNumberStyle | Style of the week numbers in the calendar. | null |
| Translations | This property is used to customize certain texts | see table below |
| Option | Description | Default Value |
|---|---|---|
| BackgroundColor | Background color of the datetimepicker control. | White |
| BorderColor | Border color of the datetimepicker control | none |
| BorderWidth | Border width of the control | 0 |
| CornerRadius | Corner radius of the control | 0 |
| FontFamily | Font family of the entry. | OpenSansRegular |
| FontSize | Font size of the entry. | 14 |
| Format | Specifies the display format for the date or time. | for date: d, for datetime: g, for time: t |
| HideIcon | Determines whether to show or hide the calendar icon. | false |
| Icon | Imagesource for the icon. | null |
| PlaceHolder | Placeholder of the entry | empty |
| PlaceHolderColor | Placeholder color of the entry | Gray |
| TextColor | Text color of the entry. | Black |
| Key | Value | Default Value |
|---|---|---|
| Hour | Hour button text in the clock view. | Hour |
| Minute | Minute button text in the clock view. | Minute |
| AM | AM button text in the clock view or in the am/pm picker. | For AM button text:<br /> in 12-hour format: AM <br />in 24-hour format: 00–11 |
| PM | PM button text in the clock view or in the am/pm picker. | For PM button text:<br /> in 12-hour format: PM<br />in 24-hour format: 12–23 |
| OK | OK button text in the dialog action buttons. If property 'OkButtonText' is not set. | OK |
| Cancel | Cancel button text in the dialog action buttons. If property 'CancelButtonText' is not set. | Cancel |
| Clear | Clear button text in the dialog action buttons. If property 'ClearButtonText' is not set. | Clear |
The SelectedDateTimeChanged event is used to indicate when a SelectedDateTime value has been changed. This event is commonly used in programming or software environments and is triggered when the SelectedDateTime value is modified.
The event utilizes the DateTimeChangedEventArgs class as its argument. The DateTimeChangedEventArgs class contains additional information that is carried at the moment the event is triggered. It may include details about the date and time change, such as the old DateTime value and the new DateTime value.
Below is an example code snippet illustrating the usage of the "SelectedDateTimeChanged" event and the "DateTimeChangedEventArgs" argument class:
.NET MAUI handler was used in the test project to remove the underline in the original entry. Please refer to the MauiProgram.cs file in the sample project. For more detailed information about handlers, please check: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/handlers/customize?view=net-maui-9.0
The Nullable DateTimePicker control is licensed under the MIT License. See <a href="LICENSE.txt">LICENSE file</a> for more information.
Contributions are welcome!
on ios, android, windows, maccatalyst
The problem of displaying the default icon in default mode has been fixed.
Various improvements.