2using System.ComponentModel;
3using System.Globalization;
4using Microsoft.Maui.Controls;
5using Microsoft.Maui.Graphics;
18 private static readonly DateTime SafeMinDate =
new DateTime(1900, 1, 1);
19 private static readonly DateTime SafeMaxDate =
new DateTime(2100, 12, 31);
24 public static readonly BindableProperty
FontProperty = BindableProperty.Create(
25 propertyName: nameof(
Font),
33 public Microsoft.Maui.Font
Font
42 public static readonly BindableProperty
XAlignProperty = BindableProperty.Create(
43 propertyName: nameof(
XAlign),
44 returnType: typeof(TextAlignment),
46 defaultValue: TextAlignment.Start);
62 returnType: typeof(
bool),
80 returnType: typeof(
string),
82 defaultValue:
string.Empty,
83 defaultBindingMode: BindingMode.OneWay);
99 returnType: typeof(Color),
101 defaultValue: Colors.Transparent);
117 returnType: typeof(Color),
119 defaultValue: Colors.Black);
135 returnType: typeof(Style),
153 returnType: typeof(DateTime?),
156 defaultBindingMode: BindingMode.TwoWay);
172 returnType: typeof(DateTime),
174 defaultValue: SafeMinDate);
182 set => this.SetValue(
MinimumDateProperty, value.SanitizeForDatePicker(SafeMinDate, SafeMaxDate));
190 returnType: typeof(DateTime),
192 defaultValue: SafeMaxDate);
200 set => this.SetValue(
MaximumDateProperty, value.SanitizeForDatePicker(SafeMinDate, SafeMaxDate));
216 this.SetDefaultDate();
219 this.picker.MinimumDate = SafeMinDate;
220 this.picker.MaximumDate = SafeMaxDate;
223 this.picker.SetBinding(DatePicker.DateProperty,
224 new Binding(nameof(this.
NullableDate), source:
this, mode: BindingMode.TwoWay));
225 this.picker.SetBinding(DatePicker.MinimumDateProperty,
226 new Binding(nameof(this.
MinimumDate), source:
this));
227 this.picker.SetBinding(DatePicker.MaximumDateProperty,
228 new Binding(nameof(this.
MaximumDate), source:
this));
229 this.picker.SetBinding(DatePicker.TextColorProperty,
230 new Binding(nameof(this.
TextColor), source:
this));
231 this.picker.SetBinding(DatePicker.StyleProperty,
232 new Binding(nameof(this.
PickerStyle), source:
this));
238 this.picker.DateSelected += this.OnPickerDateSelected;
239 if (this.picker is VisualElement E)
240 E.Focused += this.OnPickerUnfocused;
241 this.PropertyChanged += this.OnControlPropertyChanged;
243 this.CenterView = this.picker;
249 private void OnControlPropertyChanged(
object? Sender, PropertyChangedEventArgs E)
258 this.NullableDate = Clamped;
266 private void OnPickerDateSelected(
object? Sender, DateChangedEventArgs E)
269 DateTime? NewDate = E.NewDate;
271 if (NewDate.HasValue)
274 this.NullableDate = Clamped;
278 this.NullableDate =
null;
284 private void OnPickerUnfocused(
object? sender, FocusEventArgs e)
288 DateTime? PickerDate = this.picker.Date;
290 if (PickerDate.HasValue)
293 this.NullableDate = Picked;
302 private void UpdateFormat()
307 this.picker.Format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
313 string escaped = this.
Placeholder.Replace(
"'",
"''");
314 this.picker.Format = $
"'{escaped}'";
321 private void SetDefaultDate()
323 DateTime Today = DateTime.Today.SanitizeForDatePicker(SafeMinDate, SafeMaxDate);
324 this.picker.Date = Today;
Extended DatePicker for nullable values with text placeholder.
static readonly BindableProperty TextColorProperty
The TextColor property.
DateTime MaximumDate
Gets or sets the maximum allowable date.
Color TextColor
Gets or sets the color of the selected date text.
static readonly BindableProperty MaximumDateProperty
The MaximumDate property.
Color PlaceholderTextColor
Gets or sets the color of the placeholder text.
static readonly BindableProperty PlaceholderTextColorProperty
The PlaceholderTextColor property.
TextAlignment XAlign
Gets or sets the horizontal alignment of the text.
static readonly BindableProperty PlaceholderProperty
The Placeholder property.
static readonly BindableProperty PickerStyleProperty
The PickerStyle property.
bool HasBorder
Gets or sets whether the border should be shown.
CompositeDatePicker()
Initializes a new instance of the CompositeDatePicker class.
static readonly BindableProperty HasBorderProperty
The HasBorder property.
static readonly BindableProperty MinimumDateProperty
The MinimumDate property.
static readonly BindableProperty NullableDateProperty
The NullableDate property.
static readonly BindableProperty FontProperty
The Font property.
static readonly BindableProperty XAlignProperty
The XAlign property.
Style PickerStyle
Gets or sets the style applied to the internal DatePicker.
EventHandler< NullableDateChangedEventArgs >? NullableDateSelected
Occurs when the nullable date is changed by user action.
DateTime MinimumDate
Gets or sets the minimum allowable date.
Microsoft.Maui.Font Font
Gets or sets the font.
DateTime? NullableDate
Gets or sets the selected date, or null if none.
string Placeholder
Gets or sets the placeholder text to display when no date is selected.
DatePicker wrapper preventing redundant reentrant Date updates and guarding against invalid extreme d...