Skip to content

Commit 7501fc3

Browse files
Refactor SetTextLenght to avoid duplicate code.
1 parent a0fd2f8 commit 7501fc3

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

MahApps.Metro/Controls/Helper/TextBoxHelper.cs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public class TextBoxHelper
4141

4242
private static readonly DependencyProperty IsSpellCheckContextMenuEnabledProperty = DependencyProperty.RegisterAttached("IsSpellCheckContextMenuEnabled", typeof(bool), typeof(TextBoxHelper), new FrameworkPropertyMetadata(false, UseSpellCheckContextMenuChanged));
4343

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+
4448
/// <summary>
4549
/// Indicates if a TextBox or RichTextBox should use SpellCheck context menu
4650
/// </summary>
@@ -188,12 +192,6 @@ public static void SetUseFloatingWatermark(DependencyObject obj, bool value)
188192
obj.SetValue(UseFloatingWatermarkProperty, value);
189193
}
190194

191-
private static void SetTextLength(DependencyObject obj, int value)
192-
{
193-
obj.SetValue(TextLengthProperty, value);
194-
obj.SetValue(HasTextProperty, value >= 1);
195-
}
196-
197195
/// <summary>
198196
/// Gets if the attached TextBox has text.
199197
/// </summary>
@@ -207,7 +205,7 @@ public static void SetHasText(DependencyObject obj, bool value)
207205
obj.SetValue(HasTextProperty, value);
208206
}
209207

210-
static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
208+
private static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
211209
{
212210
if (d is TextBox)
213211
{
@@ -253,39 +251,42 @@ static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedE
253251
if ((bool)e.NewValue)
254252
{
255253
numericUpDown.ValueChanged += OnNumericUpDownValueChaged;
254+
numericUpDown.GotFocus += NumericUpDownGotFocus;
256255
}
257256
else
258257
{
259258
numericUpDown.ValueChanged += OnNumericUpDownValueChaged;
259+
numericUpDown.GotFocus += NumericUpDownGotFocus;
260260
}
261261
}
262262
}
263263

264-
private static void OnNumericUpDownValueChaged(object sender, RoutedPropertyChangedEventArgs<double?> e)
264+
private static void OnNumericUpDownValueChaged(object sender, RoutedEventArgs e)
265265
{
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);
270267
}
271268

272-
static void TextChanged(object sender, TextChangedEventArgs e)
269+
private static void SetTextLength<TDependencyObject>(TDependencyObject sender, Func<TDependencyObject, int> funcTextLength) where TDependencyObject : DependencyObject
273270
{
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+
}
278277
}
279278

280-
static void PasswordChanged(object sender, RoutedEventArgs e)
279+
private static void TextChanged(object sender, RoutedEventArgs e)
281280
{
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);
286287
}
287288

288-
static void TextBoxGotFocus(object sender, RoutedEventArgs e)
289+
private static void TextBoxGotFocus(object sender, RoutedEventArgs e)
289290
{
290291
var txtBox = sender as TextBox;
291292
if (txtBox == null)
@@ -295,8 +296,18 @@ static void TextBoxGotFocus(object sender, RoutedEventArgs e)
295296
txtBox.Dispatcher.BeginInvoke((Action)(txtBox.SelectAll));
296297
}
297298
}
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+
}
298309

299-
static void PasswordGotFocus(object sender, RoutedEventArgs e)
310+
private static void PasswordGotFocus(object sender, RoutedEventArgs e)
300311
{
301312
var passBox = sender as PasswordBox;
302313
if (passBox == null)

0 commit comments

Comments
 (0)