From 8a66f92089efe65cdd586c2be1c0d95f38627b0d Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Wed, 28 Sep 2022 10:16:06 +0800 Subject: [PATCH] fix: NumericUpDown issue. --- .../Controls/Input/NumericUpDown.cs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs b/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs index 81c1fa7d..9c878bb7 100644 --- a/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs +++ b/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs @@ -58,7 +58,7 @@ public class NumericUpDown : Control if (_textBox != null) { _textBox.SetBinding(SelectionBrushProperty, new Binding(SelectionBrushProperty.Name) { Source = this }); -#if !(NET40 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472) +#if NET48_OR_GREATER _textBox.SetBinding(SelectionTextBrushProperty, new Binding(SelectionTextBrushProperty.Name) { Source = this }); #endif _textBox.SetBinding(SelectionOpacityProperty, new Binding(SelectionOpacityProperty.Name) { Source = this }); @@ -66,19 +66,31 @@ public class NumericUpDown : Control _textBox.PreviewKeyDown += TextBox_PreviewKeyDown; _textBox.TextChanged += TextBox_TextChanged; + _textBox.LostFocus += TextBox_LostFocus; _textBox.Text = CurrentText; } } + private void TextBox_LostFocus(object sender, RoutedEventArgs e) + { + if (string.IsNullOrWhiteSpace(_textBox.Text)) + { + SetCurrentValue(ValueProperty, ValueBoxes.Double0Box); + } + else if (double.TryParse(_textBox.Text, out double value)) + { + SetCurrentValue(ValueProperty, value); + } + } + private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { - if (string.IsNullOrWhiteSpace(_textBox.Text)) + if (double.TryParse(_textBox.Text, out double value)) { - SetCurrentValue(ValueProperty, ValueBoxes.Int0Box); - } - else if (double.TryParse(_textBox.Text, out var value)) - { - SetCurrentValue(ValueProperty, value); + if (value >= Minimum && value <= Maximum) + { + SetCurrentValue(ValueProperty, value); + } } } @@ -328,7 +340,7 @@ public class NumericUpDown : Control set => SetValue(SelectionBrushProperty, value); } -#if !(NET40 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472) +#if NET48_OR_GREATER public static readonly DependencyProperty SelectionTextBrushProperty = TextBoxBase.SelectionTextBrushProperty.AddOwner(typeof(NumericUpDown));