Created
July 30, 2019 03:57
-
-
Save dylanberry/6d6c6a0a9a04d1e1fa885627c4fadd8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Xamarin.Forms; | |
namespace BSILabs.Controls | |
{ | |
public class NullableDatePicker : DatePicker | |
{ | |
public static readonly BindableProperty NullableDateProperty = BindableProperty.Create(nameof(NullableDate), typeof(DateTime?), typeof(NullableDatePicker), null, BindingMode.TwoWay, propertyChanged: NullableDateChanged); | |
public DateTime? NullableDate | |
{ | |
get { return (DateTime?)GetValue(NullableDateProperty); } | |
set { SetValue(NullableDateProperty, value); } | |
} | |
public static readonly BindableProperty NullTextProperty = BindableProperty.Create(nameof(NullText), typeof(string), typeof(NullableDatePicker), default(string), BindingMode.TwoWay); | |
public string NullText | |
{ | |
get { return (string)GetValue(NullTextProperty); } | |
set { SetValue(NullTextProperty, value); } | |
} | |
public NullableDatePicker() | |
{ | |
DateSelected += NullableDatePicker_DateSelected; | |
NullText = "Select Date..."; | |
} | |
private void NullableDatePicker_DateSelected(object sender, DateChangedEventArgs e) | |
{ | |
NullableDate = new DateTime( | |
e.NewDate.Year, | |
e.NewDate.Month, | |
e.NewDate.Day, | |
NullableDate?.Hour ?? 0, | |
NullableDate?.Minute ?? 0, | |
NullableDate?.Second ?? 0); | |
} | |
private static void NullableDateChanged(BindableObject obj, object oldValue, object newValue) | |
{ | |
var customDatePicker = obj as NullableDatePicker; | |
var newDate = newValue as DateTime?; | |
if (customDatePicker != null && newDate.HasValue) | |
{ | |
customDatePicker.Date = newDate.Value; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment