mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 10:06:30 +00:00
51248edbd3
allow input dots
126 lines
4.0 KiB
C#
126 lines
4.0 KiB
C#
using System.Globalization;
|
|
using System.Text.RegularExpressions;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
|
|
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 not NumericUpDown control)
|
|
return;
|
|
|
|
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;
|
|
}
|
|
|
|
private void NudTextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
|
|
{
|
|
var regex = new Regex("[^0-9.]+");
|
|
e.Handled = regex.IsMatch(e.Text);
|
|
}
|
|
|
|
private void NudTextBox_OnPasting(object sender, DataObjectPastingEventArgs e)
|
|
{
|
|
if (e.DataObject.GetDataPresent(typeof(string)))
|
|
{
|
|
var text = (string)e.DataObject.GetData(typeof(string));
|
|
var regex = new Regex("[^0-9.]+");
|
|
if (regex.IsMatch(text))
|
|
{
|
|
e.CancelCommand();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
e.CancelCommand();
|
|
}
|
|
}
|
|
} |