Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ExtendedDatePicker.cs
2{
6 public class ExtendedDatePicker : DatePicker
7 {
11 public static readonly BindableProperty FontProperty =
12 BindableProperty.Create(nameof(Font), typeof(Microsoft.Maui.Font), typeof(ExtendedDatePicker), new Microsoft.Maui.Font());
13
17 public static readonly BindableProperty NullableDateProperty =
18 BindableProperty.Create(nameof(NullableDate), typeof(DateTime?), typeof(ExtendedDatePicker), null, BindingMode.TwoWay,
19 propertyChanged: DatePropertyChanged);
20
24 public static readonly BindableProperty XAlignProperty =
25 BindableProperty.Create(nameof(XAlign), typeof(TextAlignment), typeof(ExtendedDatePicker),
26 TextAlignment.Start);
27
31 public static readonly BindableProperty HasBorderProperty =
32 BindableProperty.Create(nameof(HasBorder), typeof(bool), typeof(ExtendedDatePicker), true);
33
37 public static readonly BindableProperty PlaceholderProperty =
38 BindableProperty.Create(nameof(Placeholder), typeof(string), typeof(ExtendedDatePicker), string.Empty, BindingMode.OneWay);
39
43 public static readonly BindableProperty PlaceholderTextColorProperty =
44 BindableProperty.Create(nameof(PlaceholderTextColor), typeof(Color), typeof(ExtendedDatePicker), Colors.Transparent);
45
46
50 public Microsoft.Maui.Font Font
51 {
52 get { return (Microsoft.Maui.Font)this.GetValue(FontProperty); }
53 set { this.SetValue(FontProperty, value); }
54 }
55
59 public DateTime? NullableDate
60 {
61 get { return (DateTime?)this.GetValue(NullableDateProperty); }
62 set
63 {
64 if (value != this.NullableDate)
65 {
66 this.SetValue(NullableDateProperty, value);
67 this.UpdateDate();
68 }
69 }
70 }
71
75 public TextAlignment XAlign
76 {
77 get { return (TextAlignment)this.GetValue(XAlignProperty); }
78 set { this.SetValue(XAlignProperty, value); }
79 }
80
81
85 public bool HasBorder
86 {
87 get { return (bool)this.GetValue(HasBorderProperty); }
88 set { this.SetValue(HasBorderProperty, value); }
89 }
90
94 public string Placeholder
95 {
96 get { return (string)this.GetValue(PlaceholderProperty); }
97 set { this.SetValue(PlaceholderProperty, value); }
98 }
99
104 {
105 get { return (Color)this.GetValue(PlaceholderTextColorProperty); }
106 set { this.SetValue(PlaceholderTextColorProperty, value); }
107 }
108
112 public ExtendedDatePicker() : base()
113 {
114 this.SetDefaultDate();
115 }
116
120 public event EventHandler<NullableDateChangedEventArgs>? NullableDateSelected;
121
122 static void DatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
123 {
124 ExtendedDatePicker Picker = (ExtendedDatePicker)bindable;
125 EventHandler<NullableDateChangedEventArgs>? selected = Picker.NullableDateSelected;
126
127 selected?.Invoke(Picker, new NullableDateChangedEventArgs((DateTime?)oldValue, (DateTime?)newValue));
128 }
129
130 private bool isDefaultDateSet = false;
131
133 protected override void OnPropertyChanged(string? propertyName = null)
134 {
135 base.OnPropertyChanged(propertyName);
136
137 if (propertyName == IsFocusedProperty.PropertyName)
138 {
139 // we don't know if the date picker was closed by the Ok or Cancel button,
140 // so we presuppose it was an Ok.
141 if (!this.IsFocused)
142 {
143 this.OnPropertyChanged(DateProperty.PropertyName);
144 }
145 }
146
147 if (propertyName == DateProperty.PropertyName && !this.isDefaultDateSet)
148 {
149 this.NullableDate = this.Date;
150 }
151
152 if (propertyName == NullableDateProperty.PropertyName)
153 {
154 if (this.NullableDate.HasValue)
155 {
156 this.Date = this.NullableDate.Value;
157 }
158 }
159 }
160
161 private void UpdateDate()
162 {
163 if (this.NullableDate.HasValue)
164 {
165 this.Date = this.NullableDate.Value;
166 }
167 else
168 {
169 this.isDefaultDateSet = true;
170 this.SetDefaultDate();
171 this.isDefaultDateSet = false;
172 }
173 }
174
175 private void SetDefaultDate()
176 {
177 DateTime now = DateTime.Now;
178 this.Date = new DateTime(now.Year, now.Month, now.Day);
179 }
180 }
181}
Extended DatePicker for nullable values with text placeholder
static readonly BindableProperty PlaceholderTextColorProperty
The PlaceholderTextColor property
bool HasBorder
Gets or sets if the border should be shown or not
Microsoft.Maui.Font Font
Gets or sets the Font
static readonly BindableProperty XAlignProperty
The XAlign property
static readonly BindableProperty NullableDateProperty
The NullableDate property
override void OnPropertyChanged(string? propertyName=null)
static readonly BindableProperty PlaceholderProperty
The Placeholder property
TextAlignment XAlign
Gets or sets the X alignment of the text
EventHandler< NullableDateChangedEventArgs >? NullableDateSelected
Event sent when the date is changed.
ExtendedDatePicker()
Creates a new instance of the ExtendedDatePicker class.
DateTime? NullableDate
Get or sets the NullableDate
Color PlaceholderTextColor
Sets color for placeholder text
static readonly BindableProperty FontProperty
The font property
static readonly BindableProperty HasBorderProperty
The HasBorder property