mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 06:46:30 +00:00
101 lines
3.3 KiB
C#
101 lines
3.3 KiB
C#
using System.Globalization;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
|
|
namespace Azaion.Common.Controls;
|
|
|
|
public partial class NumericUpDown : UserControl
|
|
{
|
|
public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register(
|
|
nameof(MinValue), typeof(decimal), typeof(NumericUpDown), new PropertyMetadata(0m));
|
|
|
|
public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register(
|
|
nameof(MaxValue), typeof(decimal), typeof(NumericUpDown), new PropertyMetadata(100m));
|
|
|
|
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
|
|
nameof(Value), typeof(decimal), typeof(NumericUpDown), new PropertyMetadata(10m, OnValueChanged));
|
|
|
|
public static readonly DependencyProperty StepProperty = DependencyProperty.Register(
|
|
nameof(Step), typeof(decimal), typeof(NumericUpDown), new PropertyMetadata(1m));
|
|
|
|
public decimal MinValue
|
|
{
|
|
get => (decimal)GetValue(MinValueProperty);
|
|
set => SetValue(MinValueProperty, value);
|
|
}
|
|
|
|
public decimal MaxValue
|
|
{
|
|
get => (decimal)GetValue(MaxValueProperty);
|
|
set => SetValue(MaxValueProperty, value);
|
|
}
|
|
|
|
public decimal Value
|
|
{
|
|
get => (decimal)GetValue(ValueProperty);
|
|
set => SetValue(ValueProperty, value);
|
|
}
|
|
|
|
public decimal Step
|
|
{
|
|
get => (decimal)GetValue(StepProperty);
|
|
set => SetValue(StepProperty, value);
|
|
}
|
|
|
|
public NumericUpDown()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
if (d is NumericUpDown control)
|
|
{
|
|
control.NudTextBox.Text = ((decimal)e.NewValue).ToString(CultureInfo.InvariantCulture);
|
|
control.NudTextBox.SelectionStart = control.NudTextBox.Text.Length;
|
|
}
|
|
}
|
|
|
|
private void NudButtonUp_OnClick(object sender, RoutedEventArgs e)
|
|
{
|
|
var step = Step <= 0 ? 1m : Step;
|
|
var newVal = Math.Min(MaxValue, Value + step);
|
|
Value = newVal;
|
|
NudTextBox.Text = Value.ToString(CultureInfo.InvariantCulture);
|
|
NudTextBox.SelectionStart = NudTextBox.Text.Length;
|
|
}
|
|
|
|
private void NudButtonDown_OnClick(object sender, RoutedEventArgs e)
|
|
{
|
|
var step = Step <= 0 ? 1m : Step;
|
|
var newVal = Math.Max(MinValue, Value - step);
|
|
Value = newVal;
|
|
NudTextBox.Text = Value.ToString(CultureInfo.InvariantCulture);
|
|
NudTextBox.SelectionStart = NudTextBox.Text.Length;
|
|
}
|
|
|
|
private void NudTextBox_OnLostFocus(object sender, RoutedEventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(NudTextBox.Text) || !decimal.TryParse(NudTextBox.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out var number))
|
|
{
|
|
NudTextBox.Text = Value.ToString(CultureInfo.InvariantCulture);
|
|
return;
|
|
}
|
|
if (number > MaxValue )
|
|
{
|
|
Value = MaxValue;
|
|
NudTextBox.Text = MaxValue.ToString(CultureInfo.InvariantCulture);
|
|
}
|
|
else if (number < MinValue)
|
|
{
|
|
Value = MinValue;
|
|
NudTextBox.Text = Value.ToString(CultureInfo.InvariantCulture);
|
|
}
|
|
else
|
|
{
|
|
Value = number;
|
|
}
|
|
|
|
NudTextBox.SelectionStart = NudTextBox.Text.Length;
|
|
}
|
|
} |