|
@@ -1,4 +1,5 @@
|
|
|
using System;
|
|
|
+using System.Globalization;
|
|
|
using System.Text.RegularExpressions;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
@@ -13,39 +14,61 @@ namespace Compdfkit_Tools.Common
|
|
|
|
|
|
// The dependency property which will be accessible on the UserControl
|
|
|
public static readonly DependencyProperty UnitProperty =
|
|
|
- DependencyProperty.Register("Unit", typeof(string), typeof(NumericUpDownControl), new UIPropertyMetadata());
|
|
|
+ DependencyProperty.Register(nameof(Unit), typeof(string), typeof(NumericUpDownControl), new UIPropertyMetadata());
|
|
|
|
|
|
public string Unit
|
|
|
{
|
|
|
- get { return (string)GetValue(UnitProperty); }
|
|
|
- set { SetValue(UnitProperty, value); }
|
|
|
+ get => (string)GetValue(UnitProperty);
|
|
|
+ set => SetValue(UnitProperty, value);
|
|
|
}
|
|
|
|
|
|
// The dependency property which will be accessible on the UserControl
|
|
|
- public static readonly DependencyProperty MaxiumProperty =
|
|
|
- DependencyProperty.Register("Maxium", typeof(int), typeof(NumericUpDownControl), new UIPropertyMetadata());
|
|
|
- public int Maxium
|
|
|
+ public static readonly DependencyProperty MaximumProperty =
|
|
|
+ DependencyProperty.Register(nameof(Maximum), typeof(int), typeof(NumericUpDownControl), new UIPropertyMetadata(HandleMaxValueChanged));
|
|
|
+ public int Maximum
|
|
|
{
|
|
|
- get { return (int)GetValue(MaxiumProperty); }
|
|
|
- set { SetValue(MaxiumProperty, value); }
|
|
|
+ get => (int)GetValue(MaximumProperty);
|
|
|
+ set => SetValue(MaximumProperty, value);
|
|
|
}
|
|
|
|
|
|
// The dependency property which will be accessible on the UserControl
|
|
|
public static readonly DependencyProperty TextProperty =
|
|
|
- DependencyProperty.Register("Text", typeof(string), typeof(NumericUpDownControl), new UIPropertyMetadata());
|
|
|
+ DependencyProperty.Register(nameof(Text), typeof(string), typeof(NumericUpDownControl), new UIPropertyMetadata());
|
|
|
public string Text
|
|
|
{
|
|
|
- get { return (string)GetValue(TextProperty); }
|
|
|
- set { SetValue(TextProperty, value); }
|
|
|
+ get => (string)GetValue(TextProperty);
|
|
|
+ set => SetValue(TextProperty, value);
|
|
|
}
|
|
|
|
|
|
public static readonly DependencyProperty MinimumProperty =
|
|
|
- DependencyProperty.Register("Minimum", typeof(int), typeof(NumericUpDownControl), new UIPropertyMetadata());
|
|
|
+ DependencyProperty.Register(nameof(Minimum), typeof(int), typeof(NumericUpDownControl), new UIPropertyMetadata(HandleMinValueChanged));
|
|
|
public int Minimum
|
|
|
{
|
|
|
- get { return (int)GetValue(MinimumProperty); }
|
|
|
- set { SetValue(MinimumProperty, value); }
|
|
|
+ get => (int)GetValue(MinimumProperty);
|
|
|
+ set => SetValue(MinimumProperty, value);
|
|
|
}
|
|
|
+
|
|
|
+ // The dependency property which will be accessible on the UserControl
|
|
|
+ public static readonly DependencyProperty IsValueValidProperty =
|
|
|
+ DependencyProperty.Register(nameof(IsValueValid), typeof(bool), typeof(NumericUpDownControl), new UIPropertyMetadata());
|
|
|
+ public bool IsValueValid
|
|
|
+ {
|
|
|
+ get => (bool)GetValue(IsValueValidProperty);
|
|
|
+ set => SetValue(IsValueValidProperty, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void HandleMinValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
|
+ {
|
|
|
+ NumericUpDownControl source = (NumericUpDownControl) d;
|
|
|
+ source.Validator.MinValue = (int) e.NewValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void HandleMaxValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
|
+ {
|
|
|
+ NumericUpDownControl source = (NumericUpDownControl) d;
|
|
|
+ source.Validator.MaxValue = (int) e.NewValue;
|
|
|
+ }
|
|
|
+
|
|
|
public NumericUpDownControl()
|
|
|
{
|
|
|
InitializeComponent();
|
|
@@ -85,39 +108,94 @@ namespace Compdfkit_Tools.Common
|
|
|
|
|
|
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
|
|
|
{
|
|
|
- if (!string.IsNullOrEmpty(TextBox.Text))
|
|
|
+ if(int.TryParse(TextBox.Text, out int num))
|
|
|
{
|
|
|
- if (int.Parse(TextBox.Text) > Maxium)
|
|
|
+ if (num <= Maximum && num >= Minimum)
|
|
|
{
|
|
|
- TextBox.Text = Maxium.ToString();
|
|
|
+ IsValueValid = true;
|
|
|
+ return;
|
|
|
}
|
|
|
+ }
|
|
|
+ IsValueValid = false;
|
|
|
+ }
|
|
|
|
|
|
- if (int.Parse(TextBox.Text) < Minimum)
|
|
|
+ private void UpButton_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ if (int.TryParse(TextBox.Text, out int num))
|
|
|
+ {
|
|
|
+ int newNum = num + 1;
|
|
|
+ if (newNum > Maximum)
|
|
|
+ {
|
|
|
+ TextBox.Text = Maximum.ToString();
|
|
|
+ }
|
|
|
+ else if(newNum < Minimum)
|
|
|
{
|
|
|
TextBox.Text = Minimum.ToString();
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TextBox.Text = newNum.ToString();
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- TextBox.Text = GetMinAbsoluteValue(Minimum, Maxium).ToString();
|
|
|
+ TextBox.Text = Minimum.ToString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void UpButton_Click(object sender, RoutedEventArgs e)
|
|
|
+ private void DownButton_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
if (int.TryParse(TextBox.Text, out int num))
|
|
|
{
|
|
|
- TextBox.Text = (++num).ToString();
|
|
|
+ int newNum = num - 1;
|
|
|
+ if (newNum > Maximum)
|
|
|
+ {
|
|
|
+ TextBox.Text = Maximum.ToString();
|
|
|
+ }
|
|
|
+ else if (newNum < Minimum)
|
|
|
+ {
|
|
|
+ TextBox.Text = Minimum.ToString();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TextBox.Text = newNum.ToString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TextBox.Text = Minimum.ToString();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- private void DownButton_Click(object sender, RoutedEventArgs e)
|
|
|
+ }
|
|
|
+
|
|
|
+ public class CustomValidationRule : ValidationRule
|
|
|
+ {
|
|
|
+ public int MaxValue { get; set; }
|
|
|
+ public int MinValue { get; set; }
|
|
|
+ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
|
|
|
{
|
|
|
- if (int.TryParse(TextBox.Text, out int num))
|
|
|
+ if (value == null)
|
|
|
{
|
|
|
- TextBox.Text = (--num).ToString();
|
|
|
+ return new ValidationResult(false, "Value cannot be empty.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (int.TryParse(value.ToString(), out int num))
|
|
|
+ {
|
|
|
+ if (num <= MaxValue && num >= MinValue)
|
|
|
+ {
|
|
|
+ return new ValidationResult(true, null);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return new ValidationResult(false, $"Value must be between {MinValue} and {MaxValue}.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return new ValidationResult(false, "Value must be an integer.");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|