forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormsTimePicker.cs
146 lines (123 loc) · 3.51 KB
/
FormsTimePicker.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace Xamarin.Forms.Platform.WPF
{
public class FormsTimePicker : TextBox
{
#region Properties
public static readonly DependencyProperty TimeProperty = DependencyProperty.Register("Time", typeof(TimeSpan?), typeof(FormsTimePicker), new PropertyMetadata(null, new PropertyChangedCallback(OnTimePropertyChanged)));
public TimeSpan? Time
{
get { return (TimeSpan?)GetValue(TimeProperty); }
set { SetValue(TimeProperty, value); }
}
public static readonly DependencyProperty TimeFormatProperty = DependencyProperty.Register("TimeFormat", typeof(String), typeof(FormsTimePicker), new PropertyMetadata(@"hh\:mm", new PropertyChangedCallback(OnTimeFormatPropertyChanged)));
public String TimeFormat
{
get { return (String)GetValue(TimeFormatProperty); }
set { SetValue(TimeFormatProperty, value); }
}
#endregion
#region Events
public delegate void TimeChangedEventHandler(object sender, TimeChangedEventArgs e);
public event TimeChangedEventHandler TimeChanged;
#endregion
public FormsTimePicker()
{
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
SetText();
}
private void SetText()
{
if (Time == null)
Text = null;
else
{
var dateTime = new DateTime(Time.Value.Ticks);
String text = dateTime.ToString(String.IsNullOrWhiteSpace(TimeFormat) ? @"hh\:mm" : TimeFormat.ToLower());
if (text.CompareTo(Text) != 0)
Text = text;
}
}
private void SetTime()
{
DateTime dateTime = DateTime.MinValue;
String timeFormat = String.IsNullOrWhiteSpace(TimeFormat) ? @"hh\:mm" : TimeFormat.ToLower();
if (DateTime.TryParseExact(Text, timeFormat, null, System.Globalization.DateTimeStyles.None, out dateTime))
{
if ((Time == null) || (Time != null && Time.Value.CompareTo(dateTime.TimeOfDay) != 0))
{
if (dateTime.TimeOfDay < TimeSpan.FromHours(24) && dateTime.TimeOfDay > TimeSpan.Zero)
Time = dateTime.TimeOfDay;
else
SetText();
}
}
else
SetText();
}
#region Overrides
protected override void OnLostFocus(RoutedEventArgs e)
{
SetTime();
base.OnLostFocus(e);
}
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
}
#endregion
#region Property Changes
private static void OnTimePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FormsTimePicker element = d as FormsTimePicker;
if (element == null)
return;
element.OnTimeChanged(e.OldValue as TimeSpan?, e.NewValue as TimeSpan?);
}
private void OnTimeChanged(TimeSpan? oldValue, TimeSpan? newValue)
{
SetText();
if (TimeChanged != null)
TimeChanged(this, new TimeChangedEventArgs(oldValue, newValue));
}
private static void OnTimeFormatPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FormsTimePicker element = d as FormsTimePicker;
if (element == null)
return;
element.OnTimeFormatChanged();
}
private void OnTimeFormatChanged()
{
SetText();
}
#endregion
}
public class TimeChangedEventArgs : EventArgs
{
private TimeSpan? _oldTime;
private TimeSpan? _newTime;
public TimeSpan? OldTime
{
get { return _oldTime; }
}
public TimeSpan? NewTime
{
get { return _newTime; }
}
public TimeChangedEventArgs(TimeSpan? oldTime, TimeSpan? newTime)
{
_oldTime = oldTime;
_newTime = newTime;
}
}
}