Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/numericupdown control #1022

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Add new dp, NumericUpDown.Wrap
  • Loading branch information
koal44 committed Mar 27, 2024
commit a2493f377b4b56b4c617bc8cd2a0c4cbda3b6f7c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
MaxValue="1"
MinValue="0"
Step="0.01"
Wrap="True"
Value="0.05" />
</Grid>
</controls:ControlExample>
Expand Down
20 changes: 18 additions & 2 deletions src/Wpf.Ui/Controls/NumericUpDown/NumericUpDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ private static void OnButtonAlignmentChanged(DependencyObject d, DependencyPrope
}
}

/// <summary>
/// Gets or sets a value indicating whether the control automatically wraps the value to the Minimum or Maximum when the value exceeds the range.
/// </summary>
public bool Wrap
{
get => (bool)GetValue(WrapProperty);
set => SetValue(WrapProperty, value);
}

/// <summary>Identifies the <see cref="Wrap"/> dependency property.</summary>
public static readonly DependencyProperty WrapProperty = DependencyProperty.Register(nameof(Wrap), typeof(bool), typeof(NumericUpDown), new FrameworkPropertyMetadata(false));

private double CoerceMyValue(double val)
{
double clampedValue = Math.Max(MinValue, Math.Min(MaxValue, val));
Expand All @@ -267,15 +279,19 @@ public override void OnApplyTemplate()
{
downButton.Click += (sender, e) =>
{
Value -= Step;
double newValue = Value - Step;
newValue = Wrap && newValue < MinValue ? MaxValue : newValue;
SetCurrentValue(ValueProperty, newValue);
};
}

if (GetTemplateChild("PART_UpButton") is RepeatButton upButton)
{
upButton.Click += (sender, e) =>
{
Value += Step;
double newValue = Value + Step;
newValue = Wrap && newValue > MaxValue ? MinValue : newValue;
SetCurrentValue(ValueProperty, newValue);
};
}
}
Expand Down