Skip to content

Commit 9dd57c9

Browse files
committed
Changed the name of IsIntegerOnly to HasDecimals, so users won't think there is actually an int behind the scenes doing the work of the NumericUpDown.
Changed the CoerseValue to remove the decimals to cover cases where the decimal point came not from the keyboard itself. Fixed the HasDecimalsChanged to not act if the double? is null. It was inserting a 0, where it should be null. Updated the UI to reflect these changes.
1 parent c3888c9 commit 9dd57c9

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

MahApps.Metro/Controls/NumericUpDown.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ private static void IsReadOnlyPropertyChangedCallback(DependencyObject dependenc
142142
typeof(NumericUpDown),
143143
new PropertyMetadata(true));
144144

145-
public static readonly DependencyProperty IsIntegerOnlyProperty = DependencyProperty.Register(
146-
"IsIntegerOnly",
145+
public static readonly DependencyProperty HasDecimalsProperty = DependencyProperty.Register(
146+
"HasDecimals",
147147
typeof(bool),
148148
typeof(NumericUpDown),
149-
new FrameworkPropertyMetadata(false, OnIsIntegerOnlyChanged));
149+
new FrameworkPropertyMetadata(true, OnHasDecimalsChanged));
150150

151151
private const double DefaultInterval = 1d;
152152
private const int DefaultDelay = 500;
@@ -444,11 +444,11 @@ private CultureInfo SpecificCultureInfo
444444
/// </summary>
445445
[Bindable(true)]
446446
[Category("Common")]
447-
[DefaultValue(false)]
448-
public bool IsIntegerOnly
447+
[DefaultValue(true)]
448+
public bool HasDecimals
449449
{
450-
get { return (bool)GetValue(IsIntegerOnlyProperty); }
451-
set { SetValue(IsIntegerOnlyProperty, value); }
450+
get { return (bool)GetValue(HasDecimalsProperty); }
451+
set { SetValue(HasDecimalsProperty, value); }
452452
}
453453

454454
/// <summary>
@@ -671,7 +671,7 @@ protected void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
671671
{
672672
if (textBox.Text.All(i => i.ToString(equivalentCulture) != numberFormatInfo.NumberDecimalSeparator) || allTextSelected)
673673
{
674-
if (!IsIntegerOnly)
674+
if (HasDecimals)
675675
{
676676
e.Handled = false;
677677
}
@@ -821,6 +821,10 @@ private static object CoerceValue(DependencyObject d, object value)
821821
var numericUpDown = (NumericUpDown)d;
822822
double val = ((double?)value).Value;
823823

824+
if (numericUpDown.HasDecimals == false)
825+
{
826+
val = Math.Truncate(val);
827+
}
824828
if (val < numericUpDown.Minimum)
825829
{
826830
return numericUpDown.Minimum;
@@ -892,17 +896,15 @@ private static void OnValueChanged(DependencyObject d, DependencyPropertyChanged
892896
numericUpDown.OnValueChanged((double?)e.OldValue, (double?)e.NewValue);
893897
}
894898

895-
private static void OnIsIntegerOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
899+
private static void OnHasDecimalsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
896900
{
897901
var numericUpDown = (NumericUpDown)d;
898902
double? oldValue = numericUpDown.Value;
899903

900-
if ((bool)e.NewValue == true)
904+
if ((bool)e.NewValue == false && numericUpDown.Value != null)
901905
{
902906
numericUpDown.Value = Math.Truncate(numericUpDown.Value.GetValueOrDefault());
903907
}
904-
905-
numericUpDown.OnValueChanged(oldValue, numericUpDown.Value);
906908
}
907909

908910
private static bool ValidateDelay(object value)

samples/MetroDemo/ExampleViews/TextExamples.xaml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,37 +223,38 @@
223223
TextAlignment="Left"
224224
Minimum="0"
225225
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
226-
IsIntegerOnly="{Binding ElementName=IntegerOnlyCheckBox, Path=IsChecked, Mode=TwoWay}"
226+
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
227227
Maximum="10" />
228228

229229
<Label Content='Interval="2"' />
230230

231-
<CheckBox x:Name="IntegerOnlyCheckBox"
232-
Content="IsIntegerOnly"
231+
<CheckBox x:Name="HasDecimalsCheckBox"
232+
Content="HasDecimals"
233+
IsChecked="True"
233234
Margin="0,7" />
234235

235236
<Controls:NumericUpDown Value="5"
236237
ButtonsAlignment="Left"
237-
IsIntegerOnly="{Binding ElementName=IntegerOnlyCheckBox, Path=IsChecked, Mode=TwoWay}"
238+
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
238239
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
239240
Interval="2" />
240241

241242
<Label Content='Interval="5"' />
242243
<Controls:NumericUpDown Value="5"
243244
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
244-
IsIntegerOnly="{Binding ElementName=IntegerOnlyCheckBox, Path=IsChecked, Mode=TwoWay}"
245+
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
245246
Controls:TextBoxHelper.ClearTextButton="True"
246247
ButtonsAlignment="Left"
247248
Interval="5" />
248249

249250
<Controls:NumericUpDown Margin="{StaticResource ControlMargin}"
250251
Controls:TextBoxHelper.Watermark="No Speedup when long pressed"
251252
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
252-
IsIntegerOnly="{Binding ElementName=IntegerOnlyCheckBox, Path=IsChecked, Mode=TwoWay}"
253+
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
253254
Speedup="false" />
254255
<Controls:NumericUpDown Margin="{StaticResource ControlMargin}"
255256
Controls:TextBoxHelper.Watermark="Speedup when long pressed after 500ms"
256-
IsIntegerOnly="{Binding ElementName=IntegerOnlyCheckBox, Path=IsChecked, Mode=TwoWay}"
257+
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
257258
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
258259
Delay="500"
259260
Speedup="true" />
@@ -264,7 +265,7 @@
264265
<Controls:NumericUpDown Value="5"
265266
x:Name="test"
266267
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
267-
IsIntegerOnly="{Binding ElementName=IntegerOnlyCheckBox, Path=IsChecked, Mode=TwoWay}"
268+
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
268269
StringFormat="pcs. {0:N2} pcs."
269270
Maximum="100" />
270271
<Grid Margin="{StaticResource ControlMargin}">
@@ -298,7 +299,7 @@
298299
<Controls:NumericUpDown x:Name="StringFormatNumUpDown"
299300
Margin="{StaticResource ControlMargin}"
300301
Value="{Binding ElementName=test, Path=Value, Mode=TwoWay}"
301-
IsIntegerOnly="{Binding ElementName=IntegerOnlyCheckBox, Path=IsChecked, Mode=TwoWay}"
302+
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
302303
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
303304
Interval=".1"
304305
StringFormat="0,000.00" />

0 commit comments

Comments
 (0)