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
|
/*************************************************************************************
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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
using Xceed.Wpf.Toolkit.Primitives;
namespace Xceed.Wpf.Toolkit.Primitives
{
public abstract class DateTimeUpDownBase<T> : UpDownBase<T>
{
#region Members
internal List<DateTimeInfo> _dateTimeInfoList = new List<DateTimeInfo>();
internal DateTimeInfo _selectedDateTimeInfo;
internal bool _fireSelectionChangedEvent = true;
internal bool _processTextChanged = true;
#endregion //Members
#region Properties
#region CurrentDateTimePart
public static readonly DependencyProperty CurrentDateTimePartProperty = DependencyProperty.Register( "CurrentDateTimePart", typeof( DateTimePart )
, typeof( DateTimeUpDownBase<T> ), new UIPropertyMetadata( DateTimePart.Other, OnCurrentDateTimePartChanged ) );
public DateTimePart CurrentDateTimePart
{
get
{
return (DateTimePart)GetValue( CurrentDateTimePartProperty );
}
set
{
SetValue( CurrentDateTimePartProperty, value );
}
}
private static void OnCurrentDateTimePartChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
{
DateTimeUpDownBase<T> dateTimeUpDownBase = o as DateTimeUpDownBase<T>;
if( dateTimeUpDownBase != null )
dateTimeUpDownBase.OnCurrentDateTimePartChanged( (DateTimePart)e.OldValue, (DateTimePart)e.NewValue );
}
protected virtual void OnCurrentDateTimePartChanged( DateTimePart oldValue, DateTimePart newValue )
{
this.Select( this.GetDateTimeInfo( newValue ) );
}
#endregion //CurrentDateTimePart
#region Step
public static readonly DependencyProperty StepProperty = DependencyProperty.Register( "Step", typeof( int )
, typeof( DateTimeUpDownBase<T> ), new UIPropertyMetadata( 1, OnStepChanged ) );
public int Step
{
get
{
return (int)GetValue( StepProperty );
}
set
{
SetValue( StepProperty, value );
}
}
private static void OnStepChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
{
var dateTimeUpDownBase = o as DateTimeUpDownBase<T>;
if( dateTimeUpDownBase != null )
dateTimeUpDownBase.OnStepChanged( (int)e.OldValue, (int)e.NewValue );
}
protected virtual void OnStepChanged( int oldValue, int newValue )
{
}
#endregion //Step
#endregion
#region Constructors
internal DateTimeUpDownBase()
{
this.InitializeDateTimeInfoList( this.Value );
this.Loaded += this.DateTimeUpDownBase_Loaded;
}
#endregion
#region BaseClass Overrides
public override void OnApplyTemplate()
{
if( this.TextBox != null )
{
this.TextBox.SelectionChanged -= this.TextBox_SelectionChanged;
}
base.OnApplyTemplate();
if( this.TextBox != null )
{
this.TextBox.SelectionChanged += this.TextBox_SelectionChanged;
}
}
protected override void OnPreviewKeyDown( KeyEventArgs e )
{
int selectionStart = ( _selectedDateTimeInfo != null ) ? _selectedDateTimeInfo.StartPosition : 0;
int selectionLength = ( _selectedDateTimeInfo != null ) ? _selectedDateTimeInfo.Length : 0;
switch( e.Key )
{
case Key.Enter:
{
if( !IsReadOnly )
{
_fireSelectionChangedEvent = false;
BindingExpression binding = BindingOperations.GetBindingExpression( TextBox, System.Windows.Controls.TextBox.TextProperty );
binding.UpdateSource();
_fireSelectionChangedEvent = true;
}
return;
}
case Key.Add:
if( this.AllowSpin && !this.IsReadOnly )
{
this.DoIncrement();
e.Handled = true;
}
_fireSelectionChangedEvent = false;
break;
case Key.Subtract:
if( this.AllowSpin && !this.IsReadOnly )
{
this.DoDecrement();
e.Handled = true;
}
_fireSelectionChangedEvent = false;
break;
case Key.Right:
if( this.IsCurrentValueValid() )
{
this.PerformKeyboardSelection( selectionStart + selectionLength );
e.Handled = true;
}
_fireSelectionChangedEvent = false;
break;
case Key.Left:
if( this.IsCurrentValueValid() )
{
this.PerformKeyboardSelection( selectionStart > 0 ? selectionStart - 1 : 0 );
e.Handled = true;
}
_fireSelectionChangedEvent = false;
break;
default:
{
_fireSelectionChangedEvent = false;
break;
}
}
base.OnPreviewKeyDown( e );
}
#endregion
#region Event Hanlders
private void TextBox_SelectionChanged( object sender, RoutedEventArgs e )
{
if( _fireSelectionChangedEvent )
this.PerformMouseSelection();
else
_fireSelectionChangedEvent = true;
}
private void DateTimeUpDownBase_Loaded( object sender, RoutedEventArgs e )
{
this.InitSelection();
}
#endregion
#region Methods
protected virtual void InitializeDateTimeInfoList( T value )
{
}
protected virtual bool IsCurrentValueValid()
{
return true;
}
protected virtual void PerformMouseSelection()
{
var dateTimeInfo = this.GetDateTimeInfo( TextBox.SelectionStart );
if( (dateTimeInfo != null) && (dateTimeInfo.Type == DateTimePart.Other) )
{
this.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action( () =>
{
// Select the next dateTime part
this.Select( this.GetDateTimeInfo( dateTimeInfo.StartPosition + dateTimeInfo.Length ) );
}
) );
return;
}
this.Select( dateTimeInfo );
}
protected virtual bool IsLowerThan( T value1, T value2 )
{
return false;
}
protected virtual bool IsGreaterThan( T value1, T value2 )
{
return false;
}
internal DateTimeInfo GetDateTimeInfo( int selectionStart )
{
return _dateTimeInfoList.FirstOrDefault( ( info ) =>
( info.StartPosition <= selectionStart ) && ( selectionStart < ( info.StartPosition + info.Length ) ) );
}
internal DateTimeInfo GetDateTimeInfo( DateTimePart part )
{
return _dateTimeInfoList.FirstOrDefault( ( info ) =>info.Type == part );
}
internal void Select( DateTimeInfo info )
{
if( (info != null) && !info.Equals( _selectedDateTimeInfo ) && ( this.TextBox != null) && !string.IsNullOrEmpty( this.TextBox.Text ) )
{
_fireSelectionChangedEvent = false;
this.TextBox.Select( info.StartPosition, info.Length );
_fireSelectionChangedEvent = true;
_selectedDateTimeInfo = info;
#if VS2008
this.CurrentDateTimePart = info.Type;
#else
this.SetCurrentValue( DateTimeUpDownBase<T>.CurrentDateTimePartProperty, info.Type );
#endif
}
}
internal T CoerceValueMinMax( T value )
{
if( this.IsLowerThan( value, this.Minimum ) )
return this.Minimum;
else if( this.IsGreaterThan( value, this.Maximum ) )
return this.Maximum;
else
return value;
}
internal void ValidateDefaultMinMax( T value )
{
// DefaultValue is always accepted.
if( object.Equals( value, this.DefaultValue ) )
return;
if( this.IsLowerThan( value, this.Minimum ) )
throw new ArgumentOutOfRangeException( "Minimum", String.Format( "Value must be greater than MinValue of {0}", this.Minimum ) );
else if( this.IsGreaterThan( value, this.Maximum ) )
throw new ArgumentOutOfRangeException( "Maximum", String.Format( "Value must be less than MaxValue of {0}", this.Maximum ) );
}
internal T GetClippedMinMaxValue( T value )
{
if( this.IsGreaterThan( value, this.Maximum ) )
return this.Maximum;
else if( this.IsLowerThan( value, this.Minimum ) )
return this.Minimum;
return value;
}
protected internal virtual void PerformKeyboardSelection( int nextSelectionStart )
{
this.TextBox.Focus();
if( !this.UpdateValueOnEnterKey )
{
this.CommitInput();
}
int selectedDateStartPosition = ( _selectedDateTimeInfo != null ) ? _selectedDateTimeInfo.StartPosition : 0;
int direction = nextSelectionStart - selectedDateStartPosition;
if( direction > 0 )
{
this.Select( this.GetNextDateTimeInfo( nextSelectionStart ) );
}
else
{
this.Select( this.GetPreviousDateTimeInfo( nextSelectionStart - 1 ) );
}
}
private DateTimeInfo GetNextDateTimeInfo( int nextSelectionStart )
{
DateTimeInfo nextDateTimeInfo = this.GetDateTimeInfo( nextSelectionStart );
if( nextDateTimeInfo == null )
{
nextDateTimeInfo = _dateTimeInfoList.First();
}
DateTimeInfo initialDateTimeInfo = nextDateTimeInfo;
while( nextDateTimeInfo.Type == DateTimePart.Other )
{
nextDateTimeInfo = this.GetDateTimeInfo( nextDateTimeInfo.StartPosition + nextDateTimeInfo.Length );
if( nextDateTimeInfo == null )
{
nextDateTimeInfo = _dateTimeInfoList.First();
}
if( object.Equals( nextDateTimeInfo, initialDateTimeInfo ) )
throw new InvalidOperationException( "Couldn't find a valid DateTimeInfo." );
}
return nextDateTimeInfo;
}
private DateTimeInfo GetPreviousDateTimeInfo( int previousSelectionStart )
{
DateTimeInfo previousDateTimeInfo = this.GetDateTimeInfo( previousSelectionStart );
if( previousDateTimeInfo == null )
{
if( _dateTimeInfoList.Count > 0 )
{
previousDateTimeInfo = _dateTimeInfoList.Last();
}
}
DateTimeInfo initialDateTimeInfo = previousDateTimeInfo;
while( (previousDateTimeInfo != null) && (previousDateTimeInfo.Type == DateTimePart.Other) )
{
previousDateTimeInfo = this.GetDateTimeInfo( previousDateTimeInfo.StartPosition - 1 );
if( previousDateTimeInfo == null )
{
previousDateTimeInfo = _dateTimeInfoList.Last();
}
if( object.Equals( previousDateTimeInfo, initialDateTimeInfo ) )
throw new InvalidOperationException( "Couldn't find a valid DateTimeInfo." );
}
return previousDateTimeInfo;
}
private void InitSelection()
{
if( _selectedDateTimeInfo == null )
{
this.Select( (this.CurrentDateTimePart != DateTimePart.Other) ? this.GetDateTimeInfo( this.CurrentDateTimePart ) : this.GetDateTimeInfo( 0 ) );
}
}
#endregion
}
}
|