@@ -41,6 +41,10 @@ public class TextBoxHelper
41
41
42
42
private static readonly DependencyProperty IsSpellCheckContextMenuEnabledProperty = DependencyProperty . RegisterAttached ( "IsSpellCheckContextMenuEnabled" , typeof ( bool ) , typeof ( TextBoxHelper ) , new FrameworkPropertyMetadata ( false , UseSpellCheckContextMenuChanged ) ) ;
43
43
44
+ private static readonly Func < NumericUpDown , int > NumericUpDownTextLength = n => n . Value . HasValue ? 1 : 0 ;
45
+ private static readonly Func < PasswordBox , int > PasswordBoxTextLength = n => n . Password . Length ;
46
+ private static readonly Func < TextBox , int > TextBoxTextLength = n => n . Text . Length ;
47
+
44
48
/// <summary>
45
49
/// Indicates if a TextBox or RichTextBox should use SpellCheck context menu
46
50
/// </summary>
@@ -188,12 +192,6 @@ public static void SetUseFloatingWatermark(DependencyObject obj, bool value)
188
192
obj . SetValue ( UseFloatingWatermarkProperty , value ) ;
189
193
}
190
194
191
- private static void SetTextLength ( DependencyObject obj , int value )
192
- {
193
- obj . SetValue ( TextLengthProperty , value ) ;
194
- obj . SetValue ( HasTextProperty , value >= 1 ) ;
195
- }
196
-
197
195
/// <summary>
198
196
/// Gets if the attached TextBox has text.
199
197
/// </summary>
@@ -207,7 +205,7 @@ public static void SetHasText(DependencyObject obj, bool value)
207
205
obj . SetValue ( HasTextProperty , value ) ;
208
206
}
209
207
210
- static void OnIsMonitoringChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
208
+ private static void OnIsMonitoringChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
211
209
{
212
210
if ( d is TextBox )
213
211
{
@@ -253,39 +251,42 @@ static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedE
253
251
if ( ( bool ) e . NewValue )
254
252
{
255
253
numericUpDown . ValueChanged += OnNumericUpDownValueChaged ;
254
+ numericUpDown . GotFocus += NumericUpDownGotFocus ;
256
255
}
257
256
else
258
257
{
259
258
numericUpDown . ValueChanged += OnNumericUpDownValueChaged ;
259
+ numericUpDown . GotFocus += NumericUpDownGotFocus ;
260
260
}
261
261
}
262
262
}
263
263
264
- private static void OnNumericUpDownValueChaged ( object sender , RoutedPropertyChangedEventArgs < double ? > e )
264
+ private static void OnNumericUpDownValueChaged ( object sender , RoutedEventArgs e )
265
265
{
266
- var numericUpDown = sender as NumericUpDown ;
267
- if ( numericUpDown == null )
268
- return ;
269
- SetTextLength ( numericUpDown , numericUpDown . Value . HasValue ? 1 : 0 ) ;
266
+ SetTextLength ( sender as NumericUpDown , NumericUpDownTextLength ) ;
270
267
}
271
268
272
- static void TextChanged ( object sender , TextChangedEventArgs e )
269
+ private static void SetTextLength < TDependencyObject > ( TDependencyObject sender , Func < TDependencyObject , int > funcTextLength ) where TDependencyObject : DependencyObject
273
270
{
274
- var txtBox = sender as TextBox ;
275
- if ( txtBox == null )
276
- return ;
277
- SetTextLength ( txtBox , txtBox . Text . Length ) ;
271
+ if ( sender != null )
272
+ {
273
+ var value = funcTextLength ( sender ) ;
274
+ sender . SetValue ( TextLengthProperty , value ) ;
275
+ sender . SetValue ( HasTextProperty , value >= 1 ) ;
276
+ }
278
277
}
279
278
280
- static void PasswordChanged ( object sender , RoutedEventArgs e )
279
+ private static void TextChanged ( object sender , RoutedEventArgs e )
281
280
{
282
- var passBox = sender as PasswordBox ;
283
- if ( passBox == null )
284
- return ;
285
- SetTextLength ( passBox , passBox . Password . Length ) ;
281
+ SetTextLength ( sender as TextBox , TextBoxTextLength ) ;
282
+ }
283
+
284
+ private static void PasswordChanged ( object sender , RoutedEventArgs e )
285
+ {
286
+ SetTextLength ( sender as PasswordBox , PasswordBoxTextLength ) ;
286
287
}
287
288
288
- static void TextBoxGotFocus ( object sender , RoutedEventArgs e )
289
+ private static void TextBoxGotFocus ( object sender , RoutedEventArgs e )
289
290
{
290
291
var txtBox = sender as TextBox ;
291
292
if ( txtBox == null )
@@ -295,8 +296,18 @@ static void TextBoxGotFocus(object sender, RoutedEventArgs e)
295
296
txtBox . Dispatcher . BeginInvoke ( ( Action ) ( txtBox . SelectAll ) ) ;
296
297
}
297
298
}
299
+ private static void NumericUpDownGotFocus ( object sender , RoutedEventArgs e )
300
+ {
301
+ var numericUpDown = sender as NumericUpDown ;
302
+ if ( numericUpDown == null )
303
+ return ;
304
+ if ( GetSelectAllOnFocus ( numericUpDown ) )
305
+ {
306
+ numericUpDown . Dispatcher . BeginInvoke ( ( Action ) ( numericUpDown . SelectAll ) ) ;
307
+ }
308
+ }
298
309
299
- static void PasswordGotFocus ( object sender , RoutedEventArgs e )
310
+ private static void PasswordGotFocus ( object sender , RoutedEventArgs e )
300
311
{
301
312
var passBox = sender as PasswordBox ;
302
313
if ( passBox == null )
0 commit comments