@@ -188,12 +188,6 @@ public static void SetUseFloatingWatermark(DependencyObject obj, bool value)
188
188
obj . SetValue ( UseFloatingWatermarkProperty , value ) ;
189
189
}
190
190
191
- private static void SetTextLength ( DependencyObject obj , int value )
192
- {
193
- obj . SetValue ( TextLengthProperty , value ) ;
194
- obj . SetValue ( HasTextProperty , value >= 1 ) ;
195
- }
196
-
197
191
/// <summary>
198
192
/// Gets if the attached TextBox has text.
199
193
/// </summary>
@@ -207,7 +201,7 @@ public static void SetHasText(DependencyObject obj, bool value)
207
201
obj . SetValue ( HasTextProperty , value ) ;
208
202
}
209
203
210
- static void OnIsMonitoringChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
204
+ private static void OnIsMonitoringChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
211
205
{
212
206
if ( d is TextBox )
213
207
{
@@ -250,44 +244,67 @@ static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedE
250
244
{
251
245
var numericUpDown = d as NumericUpDown ;
252
246
numericUpDown . SelectAllOnFocus = ( bool ) e . NewValue ;
247
+ if ( ( bool ) e . NewValue )
248
+ {
249
+ numericUpDown . ValueChanged += OnNumericUpDownValueChaged ;
250
+ numericUpDown . GotFocus += NumericUpDownGotFocus ;
251
+ }
252
+ else
253
+ {
254
+ numericUpDown . ValueChanged -= OnNumericUpDownValueChaged ;
255
+ numericUpDown . GotFocus -= NumericUpDownGotFocus ;
256
+ }
253
257
}
254
258
}
255
259
256
- static void TextChanged ( object sender , TextChangedEventArgs e )
260
+ private static void SetTextLength < TDependencyObject > ( TDependencyObject sender , Func < TDependencyObject , int > funcTextLength ) where TDependencyObject : DependencyObject
257
261
{
258
- var txtBox = sender as TextBox ;
259
- if ( txtBox == null )
260
- return ;
261
- SetTextLength ( txtBox , txtBox . Text . Length ) ;
262
+ if ( sender != null )
263
+ {
264
+ var value = funcTextLength ( sender ) ;
265
+ sender . SetValue ( TextLengthProperty , value ) ;
266
+ sender . SetValue ( HasTextProperty , value >= 1 ) ;
267
+ }
262
268
}
263
269
264
- static void PasswordChanged ( object sender , RoutedEventArgs e )
270
+ private static void TextChanged ( object sender , RoutedEventArgs e )
265
271
{
266
- var passBox = sender as PasswordBox ;
267
- if ( passBox == null )
268
- return ;
269
- SetTextLength ( passBox , passBox . Password . Length ) ;
272
+ SetTextLength ( sender as TextBox , textBox => textBox . Text . Length ) ;
270
273
}
271
274
272
- static void TextBoxGotFocus ( object sender , RoutedEventArgs e )
275
+ private static void OnNumericUpDownValueChaged ( object sender , RoutedEventArgs e )
273
276
{
274
- var txtBox = sender as TextBox ;
275
- if ( txtBox == null )
276
- return ;
277
- if ( GetSelectAllOnFocus ( txtBox ) )
278
- {
279
- txtBox . Dispatcher . BeginInvoke ( ( Action ) ( txtBox . SelectAll ) ) ;
280
- }
277
+ SetTextLength ( sender as NumericUpDown , numericUpDown => numericUpDown . Value . HasValue ? 1 : 0 ) ;
278
+ }
279
+
280
+ private static void PasswordChanged ( object sender , RoutedEventArgs e )
281
+ {
282
+ SetTextLength ( sender as PasswordBox , passwordBox => passwordBox . Password . Length ) ;
281
283
}
282
284
283
- static void PasswordGotFocus ( object sender , RoutedEventArgs e )
285
+ private static void TextBoxGotFocus ( object sender , RoutedEventArgs e )
284
286
{
285
- var passBox = sender as PasswordBox ;
286
- if ( passBox == null )
287
- return ;
288
- if ( GetSelectAllOnFocus ( passBox ) )
287
+ ControlGotFocus ( sender as TextBox , textBox => textBox . SelectAll ) ;
288
+ }
289
+
290
+ private static void NumericUpDownGotFocus ( object sender , RoutedEventArgs e )
291
+ {
292
+ ControlGotFocus ( sender as NumericUpDown , numericUpDown => numericUpDown . SelectAll ) ;
293
+ }
294
+
295
+ private static void PasswordGotFocus ( object sender , RoutedEventArgs e )
296
+ {
297
+ ControlGotFocus ( sender as PasswordBox , passwordBox => passwordBox . SelectAll ) ;
298
+ }
299
+
300
+ private static void ControlGotFocus < TDependencyObject > ( TDependencyObject sender , Func < TDependencyObject , Action > funcSelectAll ) where TDependencyObject : DependencyObject
301
+ {
302
+ if ( sender != null )
289
303
{
290
- passBox . Dispatcher . BeginInvoke ( ( Action ) ( passBox . SelectAll ) ) ;
304
+ if ( GetSelectAllOnFocus ( sender ) )
305
+ {
306
+ sender . Dispatcher . BeginInvoke ( funcSelectAll , sender ) ;
307
+ }
291
308
}
292
309
}
293
310
@@ -335,8 +352,6 @@ public static void SetIsClearTextButtonBehaviorEnabled(Button obj, bool value)
335
352
obj . SetValue ( IsClearTextButtonBehaviorEnabledProperty , value ) ;
336
353
}
337
354
338
-
339
-
340
355
public static ICommand GetButtonCommand ( DependencyObject d )
341
356
{
342
357
return ( ICommand ) d . GetValue ( ButtonCommandProperty ) ;
@@ -444,22 +459,22 @@ private static void ButtonCommandOrClearTextChanged(DependencyObject d, Dependen
444
459
if ( textbox != null )
445
460
{
446
461
// only one loaded event
447
- textbox . Loaded -= TextBoxLoaded ;
448
- textbox . Loaded += TextBoxLoaded ;
462
+ textbox . Loaded -= TextChanged ;
463
+ textbox . Loaded += TextChanged ;
449
464
if ( textbox . IsLoaded )
450
465
{
451
- TextBoxLoaded ( textbox , new RoutedEventArgs ( ) ) ;
466
+ TextChanged ( textbox , new RoutedEventArgs ( ) ) ;
452
467
}
453
468
}
454
469
var passbox = d as PasswordBox ;
455
470
if ( passbox != null )
456
471
{
457
472
// only one loaded event
458
- passbox . Loaded -= PassBoxLoaded ;
459
- passbox . Loaded += PassBoxLoaded ;
473
+ passbox . Loaded -= PasswordChanged ;
474
+ passbox . Loaded += PasswordChanged ;
460
475
if ( passbox . IsLoaded )
461
476
{
462
- PassBoxLoaded ( passbox , new RoutedEventArgs ( ) ) ;
477
+ PasswordChanged ( passbox , new RoutedEventArgs ( ) ) ;
463
478
}
464
479
}
465
480
var combobox = d as ComboBox ;
@@ -483,23 +498,5 @@ static void ComboBoxLoaded(object sender, RoutedEventArgs e)
483
498
comboBox . SetValue ( HasTextProperty , ! string . IsNullOrWhiteSpace ( comboBox . Text ) || comboBox . SelectedItem != null ) ;
484
499
}
485
500
}
486
-
487
- static void PassBoxLoaded ( object sender , RoutedEventArgs e )
488
- {
489
- var passbox = sender as PasswordBox ;
490
- if ( passbox != null )
491
- {
492
- passbox . SetValue ( HasTextProperty , ! string . IsNullOrWhiteSpace ( passbox . Password ) ) ;
493
- }
494
- }
495
-
496
- static void TextBoxLoaded ( object sender , RoutedEventArgs e )
497
- {
498
- var textbox = sender as TextBox ;
499
- if ( textbox != null )
500
- {
501
- textbox . SetValue ( HasTextProperty , ! string . IsNullOrWhiteSpace ( textbox . Text ) ) ;
502
- }
503
- }
504
501
}
505
502
}
0 commit comments