| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 | 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{    /// <summary>    /// NumbericUpDown.xaml 的交互逻辑    /// </summary>    public partial class NumbericUpDown : UserControl    {        /// <summary>        /// 是否设置上下限        /// </summary>        private bool _isSetRange = false;        #region 属性        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(            "Value", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));        /// <summary>        /// 数值        /// </summary>        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));        /// <summary>        /// 最大值        /// </summary>        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));        /// <summary>        /// 最小值        /// </summary>        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));        /// <summary>        /// 步进        /// </summary>        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));        /// <summary>        /// 小数最大长度        /// </summary>        public double DigitalMaxLength        {            get            {                return (double)this.GetValue(DigitalMaxLengthProperty);            }            set            {                this.SetValue(DigitalMaxLengthProperty, value);            }        }        #endregion        /// <summary>        /// 构造函数        /// </summary>        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);        }        /// <summary>        /// 增加        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        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;            }        }        /// <summary>        /// 减少        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        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<MinValue&&_isSetRange)            {                txtInput.Text = MinValue.ToString();                Value = MinValue;            }        }    }}
 |