-
Notifications
You must be signed in to change notification settings - Fork 913
Expand file tree
/
Copy pathDateTimePicker.cs
More file actions
479 lines (392 loc) · 15.1 KB
/
Copy pathDateTimePicker.cs
File metadata and controls
479 lines (392 loc) · 15.1 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*************************************************************************************
Extended WPF Toolkit
Copyright (C) 2007-2013 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
For more features, controls, and fast professional support,
pick up the Plus Edition at http://xceed.com/wpf_toolkit
Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
***********************************************************************************/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Controls.Primitives;
using Xceed.Wpf.Toolkit.Core.Utilities;
using Xceed.Wpf.Toolkit.Primitives;
#if VS2008
using Microsoft.Windows.Controls;
using Microsoft.Windows.Controls.Primitives;
#endif
namespace Xceed.Wpf.Toolkit
{
[TemplatePart( Name = PART_Calendar, Type = typeof( Calendar ) )]
[TemplatePart( Name = PART_TimeUpDown, Type = typeof( TimePicker ) )]
public class DateTimePicker : DateTimePickerBase
{
private const string PART_Calendar = "PART_Calendar";
private const string PART_TimeUpDown = "PART_TimeUpDown";
#region Members
private Calendar _calendar;
private TimePicker _timePicker;
private DateTime? _calendarTemporaryDateTime;
private DateTime? _calendarIntendedDateTime;
#endregion //Members
#region Properties
#region AutoCloseCalendar
public static readonly DependencyProperty AutoCloseCalendarProperty = DependencyProperty.Register( "AutoCloseCalendar", typeof( bool ), typeof( DateTimePicker ), new UIPropertyMetadata( false ) );
public bool AutoCloseCalendar
{
get
{
return ( bool )GetValue( AutoCloseCalendarProperty );
}
set
{
SetValue( AutoCloseCalendarProperty, value );
}
}
#endregion //AutoCloseCalendar
#region CalendarDisplayMode
public static readonly DependencyProperty CalendarDisplayModeProperty = DependencyProperty.Register( "CalendarDisplayMode", typeof( CalendarMode )
, typeof( DateTimePicker ), new UIPropertyMetadata( CalendarMode.Month ) );
public CalendarMode CalendarDisplayMode
{
get
{
return (CalendarMode)GetValue( CalendarDisplayModeProperty );
}
set
{
SetValue( CalendarDisplayModeProperty, value );
}
}
#endregion //CalendarDisplayMode
#region CalendarWidth
public static readonly DependencyProperty CalendarWidthProperty = DependencyProperty.Register( "CalendarWidth", typeof( double )
, typeof( DateTimePicker ), new UIPropertyMetadata( 178d ) );
public double CalendarWidth
{
get
{
return ( double )GetValue( CalendarWidthProperty );
}
set
{
SetValue( CalendarWidthProperty, value );
}
}
#endregion //CalendarWidth
#region TimeFormat
public static readonly DependencyProperty TimeFormatProperty = DependencyProperty.Register( "TimeFormat", typeof( DateTimeFormat ), typeof( DateTimePicker ), new UIPropertyMetadata( DateTimeFormat.ShortTime ) );
public DateTimeFormat TimeFormat
{
get
{
return ( DateTimeFormat )GetValue( TimeFormatProperty );
}
set
{
SetValue( TimeFormatProperty, value );
}
}
#endregion //TimeFormat
#region TimeFormatString
public static readonly DependencyProperty TimeFormatStringProperty = DependencyProperty.Register( "TimeFormatString", typeof( string ), typeof( DateTimePicker ), new UIPropertyMetadata( default( String ) ), IsTimeFormatStringValid );
public string TimeFormatString
{
get
{
return ( string )GetValue( TimeFormatStringProperty );
}
set
{
SetValue( TimeFormatStringProperty, value );
}
}
private static bool IsTimeFormatStringValid(object value)
{
return DateTimeUpDown.IsFormatStringValid( value );
}
#endregion //TimeFormatString
#region TimePickerAllowSpin
public static readonly DependencyProperty TimePickerAllowSpinProperty = DependencyProperty.Register( "TimePickerAllowSpin", typeof( bool ), typeof( DateTimePicker ), new UIPropertyMetadata( true ) );
public bool TimePickerAllowSpin
{
get
{
return (bool)GetValue( TimePickerAllowSpinProperty );
}
set
{
SetValue( TimePickerAllowSpinProperty, value );
}
}
#endregion //TimePickerAllowSpin
#region TimePickerShowButtonSpinner
public static readonly DependencyProperty TimePickerShowButtonSpinnerProperty = DependencyProperty.Register( "TimePickerShowButtonSpinner", typeof( bool ), typeof( DateTimePicker ), new UIPropertyMetadata( true ) );
public bool TimePickerShowButtonSpinner
{
get
{
return (bool)GetValue( TimePickerShowButtonSpinnerProperty );
}
set
{
SetValue( TimePickerShowButtonSpinnerProperty, value );
}
}
#endregion //TimePickerShowButtonSpinner
#region TimePickerVisibility
public static readonly DependencyProperty TimePickerVisibilityProperty = DependencyProperty.Register( "TimePickerVisibility", typeof( Visibility ), typeof( DateTimePicker ), new UIPropertyMetadata( Visibility.Visible ) );
public Visibility TimePickerVisibility
{
get
{
return ( Visibility )GetValue( TimePickerVisibilityProperty );
}
set
{
SetValue( TimePickerVisibilityProperty, value );
}
}
#endregion //TimePickerVisibility
#region TimeWatermark
public static readonly DependencyProperty TimeWatermarkProperty = DependencyProperty.Register( "TimeWatermark", typeof( object ), typeof( DateTimePicker ), new UIPropertyMetadata( null ) );
public object TimeWatermark
{
get
{
return ( object )GetValue( TimeWatermarkProperty );
}
set
{
SetValue( TimeWatermarkProperty, value );
}
}
#endregion //TimeWatermark
#region TimeWatermarkTemplate
public static readonly DependencyProperty TimeWatermarkTemplateProperty = DependencyProperty.Register( "TimeWatermarkTemplate", typeof( DataTemplate ), typeof( DateTimePicker ), new UIPropertyMetadata( null ) );
public DataTemplate TimeWatermarkTemplate
{
get
{
return ( DataTemplate )GetValue( TimeWatermarkTemplateProperty );
}
set
{
SetValue( TimeWatermarkTemplateProperty, value );
}
}
#endregion //TimeWatermarkTemplate
#endregion //Properties
#region Constructors
static DateTimePicker()
{
DefaultStyleKeyProperty.OverrideMetadata( typeof( DateTimePicker ), new FrameworkPropertyMetadata( typeof( DateTimePicker ) ) );
UpdateValueOnEnterKeyProperty.OverrideMetadata( typeof( DateTimePicker ), new FrameworkPropertyMetadata( true ) );
}
public DateTimePicker()
{
}
#endregion //Constructors
#region Base Class Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if( _calendar != null )
_calendar.SelectedDatesChanged -= Calendar_SelectedDatesChanged;
_calendar = GetTemplateChild( PART_Calendar ) as Calendar;
if( _calendar != null )
{
_calendar.SelectedDatesChanged += Calendar_SelectedDatesChanged;
_calendar.SelectedDate = Value ?? null;
_calendar.DisplayDate = Value ?? this.ContextNow;
this.SetBlackOutDates();
}
if( _timePicker != null )
{
_timePicker.ValueChanged -= this.TimePicker_ValueChanged;
}
_timePicker = GetTemplateChild( PART_TimeUpDown ) as TimePicker;
if( _timePicker != null )
{
_timePicker.ValueChanged += this.TimePicker_ValueChanged;
}
}
protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)
{
ClosePopup(true);
base.OnPreviewMouseDoubleClick(e);
}
protected override void OnPreviewMouseUp( MouseButtonEventArgs e )
{
if( Mouse.Captured is CalendarItem)
{
Mouse.Capture( null );
// Do not close calendar on Year/Month Selection. Close only on Day selection.
if( AutoCloseCalendar && (_calendar != null) && ( _calendar.DisplayMode == CalendarMode.Month ) )
{
ClosePopup( true );
}
}
base.OnPreviewMouseUp( e );
}
protected override void OnValueChanged( DateTime? oldValue, DateTime? newValue )
{
//The calendar only select the Date part, not the time part.
DateTime? newValueDate = (newValue != null)
? newValue.Value.Date
: (DateTime?)null;
if( _calendar != null && _calendar.SelectedDate != newValueDate)
{
_calendar.SelectedDate = newValueDate;
_calendar.DisplayDate = newValue.GetValueOrDefault( this.ContextNow );
}
//If we change any part of the datetime without
//using the calendar when the actual date is temporary,
//clear the temporary value.
if( (_calendar != null) && (_calendarTemporaryDateTime != null) && (newValue != _calendarTemporaryDateTime ))
{
_calendarTemporaryDateTime = null;
_calendarIntendedDateTime = null;
}
base.OnValueChanged( oldValue, newValue );
}
protected override void OnIsOpenChanged( bool oldValue, bool newValue )
{
base.OnIsOpenChanged( oldValue, newValue );
if( !newValue )
{
_calendarTemporaryDateTime = null;
_calendarIntendedDateTime = null;
}
}
protected override void OnPreviewKeyDown( KeyEventArgs e )
{
//if the calendar is open then we don't want to modify the behavior of navigating the calendar control with the Up/Down keys.
if( !IsOpen )
base.OnPreviewKeyDown( e );
}
protected override void OnMaximumChanged( DateTime? oldValue, DateTime? newValue )
{
base.OnMaximumChanged( oldValue, newValue );
this.SetBlackOutDates();
}
protected override void OnMinimumChanged( DateTime? oldValue, DateTime? newValue )
{
base.OnMinimumChanged( oldValue, newValue );
this.SetBlackOutDates();
}
#endregion //Base Class Overrides
#region Event Handlers
protected override void HandleKeyDown( object sender, KeyEventArgs e )
{
// The base call will handle the Ctrl+Down, Enter and Esc keys
// in order to open or close the popup.
// Do not close the Calendar if the call is handled
// by the TimePicker inside the DateTimePicker template.
if( IsOpen
&& ( _timePicker != null)
&& _timePicker.IsKeyboardFocusWithin
&& ( _timePicker.IsOpen || e.Handled ) )
return;
base.HandleKeyDown( sender, e );
}
private void TimePicker_ValueChanged( object sender, RoutedPropertyChangedEventArgs<object> e )
{
e.Handled = true;
// if UpdateValueOnEnterKey is true,
// Sync Value on Text only when Enter Key is pressed.
if( this.UpdateValueOnEnterKey )
{
var newTime = e.NewValue as DateTime?;
if( newTime != null )
{
_fireSelectionChangedEvent = false;
var currentDate = this.ConvertTextToValue( this.TextBox.Text );
var date = currentDate ?? this.ContextNow;
var newValue = new DateTime( date.Year, date.Month, date.Day, newTime.Value.Hour, newTime.Value.Minute, newTime.Value.Second, newTime.Value.Millisecond, date.Kind );
this.TextBox.Text = newValue.ToString( this.GetFormatString( this.Format ), this.CultureInfo );
_fireSelectionChangedEvent = true;
}
}
}
private void Calendar_SelectedDatesChanged( object sender, SelectionChangedEventArgs e )
{
if( e.AddedItems.Count > 0 )
{
var newDate = ( DateTime? )e.AddedItems[ 0 ];
if( newDate != null )
{
//The Calendar will always return a date with an "Unspecified" Kind.
//Force the expected kind to the value.
newDate = DateTime.SpecifyKind( newDate.Value, this.Kind );
// Only change the year, month, and day part of the value. Keep everything to the last "tick."
// "Milliseconds" aren't precise enough. Use a mathematical scheme instead.
if( _calendarIntendedDateTime != null )
{
newDate = newDate.Value.Date + _calendarIntendedDateTime.Value.TimeOfDay;
_calendarTemporaryDateTime = null;
_calendarIntendedDateTime = null;
}
else if( Value != null )
{
newDate = newDate.Value.Date + Value.Value.TimeOfDay;
}
// Always be sure that the time part of the selected value is always
// within the bound of the min max. The time part could be altered
// if the calendar's selected date match the Minimum or Maximum date.
// Keep in memory the intended time of day, in case that the selected
// calendar date is only transitory (browsing the calendar with the keyboard)
var limitedDateTime = this.GetClippedMinMaxValue( newDate );
if( limitedDateTime.Value != newDate.Value )
{
_calendarTemporaryDateTime = limitedDateTime;
_calendarIntendedDateTime = newDate;
newDate = limitedDateTime;
}
}
if( this.UpdateValueOnEnterKey )
{
_fireSelectionChangedEvent = false;
this.TextBox.Text = newDate.Value.ToString( this.GetFormatString( this.Format ), this.CultureInfo );
_fireSelectionChangedEvent = true;
}
else
{
if( !object.Equals( newDate, Value ) )
{
this.Value = newDate;
}
}
}
}
protected override void Popup_Opened( object sender, EventArgs e )
{
base.Popup_Opened( sender, e );
if( _calendar != null )
_calendar.Focus();
}
#endregion //Event Handlers
#region Methods
private void SetBlackOutDates()
{
if( _calendar != null )
{
_calendar.BlackoutDates.Clear();
if( ( this.Minimum != null ) && this.Minimum.HasValue && ( this.Minimum.Value != DateTime.MinValue ) )
{
DateTime minDate = this.Minimum.Value;
_calendar.BlackoutDates.Add( new CalendarDateRange( DateTime.MinValue, minDate.AddDays( -1 ) ) );
}
if( ( this.Maximum != null ) && this.Maximum.HasValue && ( this.Maximum.Value != DateTime.MaxValue ) )
{
DateTime maxDate = this.Maximum.Value;
_calendar.BlackoutDates.Add( new CalendarDateRange( maxDate.AddDays( 1 ), DateTime.MaxValue ) );
}
}
}
#endregion //Methods
}
}