Browse Source

NumericUpDpwn: fixed minor bugs and added support for Percent formatting

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
866ba36b77
  1. 30
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/NumericUpDown.cs
  2. 10
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml
  3. 42
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/UpDownBase/UpDownBase.cs

30
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
}
}

10
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml

@ -1059,17 +1059,17 @@
<ControlTemplate TargetType="{x:Type local:ButtonSpinner}">
<Grid>
<Border x:Name="ElementContainer" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<DockPanel>
<DockPanel Focusable="False">
<Grid DockPanel.Dock="Right">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="1" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<RepeatButton x:Name="IncreaseButton" SnapsToDevicePixels="True" Grid.Row="0" Focusable="False" Style="{StaticResource SpinButtonStyle}" ContentTemplate="{StaticResource IncreaseGlyph}" ClickMode="Press" />
<RepeatButton x:Name="DecreaseButton" SnapsToDevicePixels="True" Grid.Row="2" Focusable="False" Style="{StaticResource SpinButtonStyle}" ContentTemplate="{StaticResource DecreaseGlyph}" ClickMode="Press" />
<RepeatButton x:Name="IncreaseButton" SnapsToDevicePixels="True" Grid.Row="0" Style="{StaticResource SpinButtonStyle}" ContentTemplate="{StaticResource IncreaseGlyph}" ClickMode="Press" />
<RepeatButton x:Name="DecreaseButton" SnapsToDevicePixels="True" Grid.Row="2" Style="{StaticResource SpinButtonStyle}" ContentTemplate="{StaticResource DecreaseGlyph}" ClickMode="Press" />
</Grid>
<ContentControl x:Name="presentationSite"
<ContentControl x:Name="presentationSite" Focusable="False"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}" />
@ -1089,7 +1089,7 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:NumericUpDown}">
<local:ButtonSpinner x:Name="Spinner">
<local:ButtonSpinner x:Name="Spinner" IsTabStop="False">
<TextBox x:Name="Text" BorderThickness="0"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"

42
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/UpDownBase/UpDownBase.cs

@ -68,13 +68,8 @@ namespace Microsoft.Windows.Controls
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
NumericUpDown nud = (NumericUpDown)d;
nud.SyncTextAndValueProperties(e.Property, e.NewValue);
}
protected virtual void OnTextChanged(string oldValue, string newValue)
{
UpDownBase<T> udb = (UpDownBase<T>)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<T>.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;
}

Loading…
Cancel
Save