Skip to content

Instantly share code, notes, and snippets.

@dylanberry
Created July 30, 2019 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanberry/6d6c6a0a9a04d1e1fa885627c4fadd8f to your computer and use it in GitHub Desktop.
Save dylanberry/6d6c6a0a9a04d1e1fa885627c4fadd8f to your computer and use it in GitHub Desktop.
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