diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/NumericUpDown.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/NumericUpDown.cs index ffd3b7e4..b2b9dd9f 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/NumericUpDown.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/NumericUpDown.cs @@ -11,7 +11,7 @@ namespace Microsoft.Windows.Controls #region Minimum - public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0d, OnMinimumPropertyChanged)); + public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(Double.MinValue, OnMinimumPropertyChanged)); public double Minimum { get { return (double)GetValue(MinimumProperty); } @@ -29,7 +29,7 @@ namespace Microsoft.Windows.Controls #region Maximum - public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(100d, OnMaximumPropertyChanged)); + public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(Double.MaxValue, OnMaximumPropertyChanged)); public double Maximum { get { return (double)GetValue(MaximumProperty); } @@ -113,7 +113,11 @@ namespace Microsoft.Windows.Controls protected override double ParseValue(string text) { - return double.Parse(text, NumberStyles.Any, CultureInfo.CurrentCulture); + NumberFormatInfo info = NumberFormatInfo.GetInstance(CultureInfo.CurrentCulture); + if (text.Contains(info.PercentSymbol)) + return TryParcePercent(text, info); + else + return TryParceDouble(text, info); } protected internal override string FormatValue() @@ -158,6 +162,26 @@ namespace Microsoft.Windows.Controls } } + private double TryParcePercent(string text, NumberFormatInfo info) + { + double result; + text = text.Replace(info.PercentSymbol, null); + result = TryParceDouble(text, info); + return result / 100; + } + + private double TryParceDouble(string text, NumberFormatInfo info) + { + double result; + if (!double.TryParse(text, NumberStyles.Any, info, out result)) + { + //an error occured now lets reset our value, text, and the text in the textbox + result = Value; + TextBox.Text = Text = FormatValue(); + } + return result; + } + #endregion //Methods } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml index cc768c0a..b2b3f717 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml @@ -1059,17 +1059,17 @@ - + - - + + - @@ -1089,7 +1089,7 @@ - + udb = (UpDownBase)d; + udb.SyncTextAndValueProperties(e.Property, e.NewValue); } #endregion //Text @@ -110,17 +105,8 @@ namespace Microsoft.Windows.Controls get { return _spinner; } private set { - if (_spinner != null) - { - _spinner.Spin -= OnSpinnerSpin; - } - _spinner = value; - - if (_spinner != null) - { - _spinner.Spin += OnSpinnerSpin; - } + _spinner.Spin += OnSpinnerSpin; } } @@ -128,9 +114,7 @@ namespace Microsoft.Windows.Controls #region Constructors - protected UpDownBase() - { - } + protected UpDownBase() { } #endregion //Constructors @@ -163,6 +147,11 @@ namespace Microsoft.Windows.Controls e.Handled = true; break; } + case Key.Enter: + { + SyncTextAndValueProperties(UpDownBase.TextProperty, TextBox.Text); + break; + } } } @@ -262,7 +251,7 @@ namespace Microsoft.Windows.Controls } } - private void SyncTextAndValueProperties(DependencyProperty p, object newValue) + protected void SyncTextAndValueProperties(DependencyProperty p, object newValue) { //prevents recursive syncing properties if (_isSyncingTextAndValueProperties) @@ -270,15 +259,16 @@ namespace Microsoft.Windows.Controls _isSyncingTextAndValueProperties = true; - if (NumericUpDown.ValueProperty == p) - { - SetValue(NumericUpDown.TextProperty, FormatValue()); - } - else if (NumericUpDown.TextProperty == p) + //this only occures when the user typed in the value + if (NumericUpDown.TextProperty == p) { SetValue(NumericUpDown.ValueProperty, ParseValue(newValue.ToString())); } + //we need to update the text no matter what because the user could have used the spin buttons to change dthe value + //or typed in the textbox so we need to reformat the entered value. + SetValue(NumericUpDown.TextProperty, FormatValue()); + _isSyncingTextAndValueProperties = false; }