using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MECF.Framework.UI.Core.Control
{
///
/// NumbericUpDown.xaml 的交互逻辑
///
public partial class NumbericUpDown : UserControl
{
///
/// 是否设置上下限
///
private bool _isSetRange = false;
#region 属性
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
///
/// 数值
///
public double Value
{
get
{
return (double)this.GetValue(ValueProperty);
}
set
{
this.SetValue(ValueProperty, value);
}
}
public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register(
"MaxValue", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
///
/// 最大值
///
public double MaxValue
{
get
{
return (double)this.GetValue(MaxValueProperty);
}
set
{
_isSetRange = true;
this.SetValue(MaxValueProperty, value);
}
}
public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register(
"MinValue", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
///
/// 最小值
///
public double MinValue
{
get
{
return (double)this.GetValue(MinValueProperty);
}
set
{
_isSetRange = true;
this.SetValue(MinValueProperty, value);
}
}
public static readonly DependencyProperty IncrementValueProperty = DependencyProperty.Register(
"IncrementValue", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.01, FrameworkPropertyMetadataOptions.AffectsRender));
///
/// 步进
///
public double IncrementValue
{
get
{
return (double)this.GetValue(IncrementValueProperty);
}
set
{
this.SetValue(IncrementValueProperty, value);
}
}
public static readonly DependencyProperty DigitalMaxLengthProperty = DependencyProperty.Register(
"DigitalMaxLength", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
///
/// 小数最大长度
///
public double DigitalMaxLength
{
get
{
return (double)this.GetValue(DigitalMaxLengthProperty);
}
set
{
this.SetValue(DigitalMaxLengthProperty, value);
}
}
#endregion
///
/// 构造函数
///
public NumbericUpDown()
{
InitializeComponent();
}
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsTextAllowed(e.Text, ((TextBox)sender).Text);
}
private bool IsTextAllowed(string key, string text)
{
Regex regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
return !regex.IsMatch(key);
}
///
/// 增加
///
///
///
private void Up_Click(object sender, RoutedEventArgs e)
{
double tmp = Value + IncrementValue;
if (_isSetRange)
{
if (tmp <= MaxValue)
{
Value = tmp;
}
else
{
Value = MaxValue;
}
}
else
{
Value = tmp;
}
}
///
/// 减少
///
///
///
private void Down_Click(object sender, RoutedEventArgs e)
{
double tmp = Value - IncrementValue;
if (_isSetRange)
{
if (tmp >= MinValue)
{
Value = tmp;
}
else
{
Value = MinValue;
}
}
else
{
Value = tmp;
}
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
double.TryParse(txtInput.Text, out var tmp);
tmp = Math.Round(tmp, 2);
if(tmp>MaxValue&&_isSetRange)
{
txtInput.Text = MaxValue.ToString();
Value = MaxValue;
}
else if(tmp