fix: NumericUpDown issue.

This commit is contained in:
NaBian committed 2022-09-28 11:44:26 +08:00
1 parent d664289be6
commit 8a66f92089
1 file changed
+20 -8
@@ -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));