Skip to content

Commit 2956f79

Browse files
committed
Added DecimalPlaces property to NumericUpDown control
1 parent 45b0281 commit 2956f79

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs

+17-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ public class NumericUpDown : TemplatedControl
119119
public static readonly StyledProperty<VerticalAlignment> VerticalContentAlignmentProperty =
120120
ContentControl.VerticalContentAlignmentProperty.AddOwner<NumericUpDown>();
121121

122+
/// <summary>
123+
/// Defines the <see cref="DecimalPlaces"/> property.
124+
/// </summary>
125+
public static readonly StyledProperty<int> DecimalPlacesProperty =
126+
AvaloniaProperty.Register<NumericUpDown, int>(nameof(DecimalPlaces), 2);
127+
122128
private IDisposable _textBoxTextChangedSubscription;
123129

124130
private double _value;
@@ -261,6 +267,15 @@ public double Value
261267
}
262268
}
263269

270+
/// <summary>
271+
/// Gets or sets the number of decimal places to display in the up-down control.
272+
/// </summary>
273+
public int DecimalPlaces
274+
{
275+
get { return GetValue(DecimalPlacesProperty); }
276+
set { SetValue(DecimalPlacesProperty, value); }
277+
}
278+
264279
/// <summary>
265280
/// Gets or sets the object to use as a watermark if the <see cref="Value"/> is null.
266281
/// </summary>
@@ -620,7 +635,7 @@ private string ConvertValueToText()
620635
/// </summary>
621636
private void OnIncrement()
622637
{
623-
var result = Value + Increment;
638+
var result = Math.Round(Value + Increment, DecimalPlaces);
624639
Value = MathUtilities.Clamp(result, Minimum, Maximum);
625640
}
626641

@@ -629,7 +644,7 @@ private void OnIncrement()
629644
/// </summary>
630645
private void OnDecrement()
631646
{
632-
var result = Value - Increment;
647+
var result = Math.Round(Value - Increment, DecimalPlaces);
633648
Value = MathUtilities.Clamp(result, Minimum, Maximum);
634649
}
635650

0 commit comments

Comments
 (0)