1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
using System;
using Xamarin.Forms.Platform;
namespace Xamarin.Forms
{
[RenderWith(typeof(_DatePickerRenderer))]
public class DatePicker : View
{
public static readonly BindableProperty FormatProperty = BindableProperty.Create(nameof(Format), typeof(string), typeof(DatePicker), "d");
public static readonly BindableProperty DateProperty = BindableProperty.Create(nameof(Date), typeof(DateTime), typeof(DatePicker), DateTime.Today, BindingMode.TwoWay, coerceValue: CoerceDate,
propertyChanged: DatePropertyChanged);
public static readonly BindableProperty MinimumDateProperty = BindableProperty.Create(nameof(MinimumDate), typeof(DateTime), typeof(DatePicker), new DateTime(1900, 1, 1),
validateValue: ValidateMinimumDate, coerceValue: CoerceMinimumDate);
public static readonly BindableProperty MaximumDateProperty = BindableProperty.Create(nameof(MaximumDate), typeof(DateTime), typeof(DatePicker), new DateTime(2100, 12, 31),
validateValue: ValidateMaximumDate, coerceValue: CoerceMaximumDate);
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(DatePicker), Color.Default);
public DateTime Date
{
get { return (DateTime)GetValue(DateProperty); }
set { SetValue(DateProperty, value); }
}
public string Format
{
get { return (string)GetValue(FormatProperty); }
set { SetValue(FormatProperty, value); }
}
public DateTime MaximumDate
{
get { return (DateTime)GetValue(MaximumDateProperty); }
set { SetValue(MaximumDateProperty, value); }
}
public DateTime MinimumDate
{
get { return (DateTime)GetValue(MinimumDateProperty); }
set { SetValue(MinimumDateProperty, value); }
}
public Color TextColor
{
get { return (Color)GetValue(TextColorProperty); }
set { SetValue(TextColorProperty, value); }
}
public event EventHandler<DateChangedEventArgs> DateSelected;
static object CoerceDate(BindableObject bindable, object value)
{
var picker = (DatePicker)bindable;
DateTime dateValue = ((DateTime)value).Date;
if (dateValue > picker.MaximumDate)
dateValue = picker.MaximumDate;
if (dateValue < picker.MinimumDate)
dateValue = picker.MinimumDate;
return dateValue;
}
static object CoerceMaximumDate(BindableObject bindable, object value)
{
DateTime dateValue = ((DateTime)value).Date;
var picker = (DatePicker)bindable;
if (picker.Date > dateValue)
picker.Date = dateValue;
return dateValue;
}
static object CoerceMinimumDate(BindableObject bindable, object value)
{
DateTime dateValue = ((DateTime)value).Date;
var picker = (DatePicker)bindable;
if (picker.Date < dateValue)
picker.Date = dateValue;
return dateValue;
}
static void DatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var datePicker = (DatePicker)bindable;
EventHandler<DateChangedEventArgs> selected = datePicker.DateSelected;
if (selected != null)
selected(datePicker, new DateChangedEventArgs((DateTime)oldValue, (DateTime)newValue));
}
static bool ValidateMaximumDate(BindableObject bindable, object value)
{
return (DateTime)value >= ((DatePicker)bindable).MinimumDate;
}
static bool ValidateMinimumDate(BindableObject bindable, object value)
{
return (DateTime)value <= ((DatePicker)bindable).MaximumDate;
}
}
}
|