NumbericUpDown.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace MECF.Framework.UI.Core.Control
  17. {
  18. /// <summary>
  19. /// NumbericUpDown.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class NumbericUpDown : UserControl
  22. {
  23. /// <summary>
  24. /// 是否设置上下限
  25. /// </summary>
  26. private bool _isSetRange = false;
  27. #region 属性
  28. public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
  29. "Value", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
  30. /// <summary>
  31. /// 数值
  32. /// </summary>
  33. public double Value
  34. {
  35. get
  36. {
  37. return (double)this.GetValue(ValueProperty);
  38. }
  39. set
  40. {
  41. this.SetValue(ValueProperty, value);
  42. }
  43. }
  44. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register(
  45. "MaxValue", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
  46. /// <summary>
  47. /// 最大值
  48. /// </summary>
  49. public double MaxValue
  50. {
  51. get
  52. {
  53. return (double)this.GetValue(MaxValueProperty);
  54. }
  55. set
  56. {
  57. _isSetRange = true;
  58. this.SetValue(MaxValueProperty, value);
  59. }
  60. }
  61. public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register(
  62. "MinValue", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
  63. /// <summary>
  64. /// 最小值
  65. /// </summary>
  66. public double MinValue
  67. {
  68. get
  69. {
  70. return (double)this.GetValue(MinValueProperty);
  71. }
  72. set
  73. {
  74. _isSetRange = true;
  75. this.SetValue(MinValueProperty, value);
  76. }
  77. }
  78. public static readonly DependencyProperty IncrementValueProperty = DependencyProperty.Register(
  79. "IncrementValue", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.01, FrameworkPropertyMetadataOptions.AffectsRender));
  80. /// <summary>
  81. /// 步进
  82. /// </summary>
  83. public double IncrementValue
  84. {
  85. get
  86. {
  87. return (double)this.GetValue(IncrementValueProperty);
  88. }
  89. set
  90. {
  91. this.SetValue(IncrementValueProperty, value);
  92. }
  93. }
  94. public static readonly DependencyProperty DigitalMaxLengthProperty = DependencyProperty.Register(
  95. "DigitalMaxLength", typeof(double), typeof(NumbericUpDown), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
  96. /// <summary>
  97. /// 小数最大长度
  98. /// </summary>
  99. public double DigitalMaxLength
  100. {
  101. get
  102. {
  103. return (double)this.GetValue(DigitalMaxLengthProperty);
  104. }
  105. set
  106. {
  107. this.SetValue(DigitalMaxLengthProperty, value);
  108. }
  109. }
  110. #endregion
  111. /// <summary>
  112. /// 构造函数
  113. /// </summary>
  114. public NumbericUpDown()
  115. {
  116. InitializeComponent();
  117. }
  118. private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
  119. {
  120. e.Handled = !IsTextAllowed(e.Text, ((TextBox)sender).Text);
  121. }
  122. private bool IsTextAllowed(string key, string text)
  123. {
  124. Regex regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
  125. return !regex.IsMatch(key);
  126. }
  127. /// <summary>
  128. /// 增加
  129. /// </summary>
  130. /// <param name="sender"></param>
  131. /// <param name="e"></param>
  132. private void Up_Click(object sender, RoutedEventArgs e)
  133. {
  134. double tmp = Value + IncrementValue;
  135. if (_isSetRange)
  136. {
  137. if (tmp <= MaxValue)
  138. {
  139. Value = tmp;
  140. }
  141. else
  142. {
  143. Value = MaxValue;
  144. }
  145. }
  146. else
  147. {
  148. Value = tmp;
  149. }
  150. }
  151. /// <summary>
  152. /// 减少
  153. /// </summary>
  154. /// <param name="sender"></param>
  155. /// <param name="e"></param>
  156. private void Down_Click(object sender, RoutedEventArgs e)
  157. {
  158. double tmp = Value - IncrementValue;
  159. if (_isSetRange)
  160. {
  161. if (tmp >= MinValue)
  162. {
  163. Value = tmp;
  164. }
  165. else
  166. {
  167. Value = MinValue;
  168. }
  169. }
  170. else
  171. {
  172. Value = tmp;
  173. }
  174. }
  175. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  176. {
  177. double.TryParse(txtInput.Text, out var tmp);
  178. tmp = Math.Round(tmp, 2);
  179. if(tmp>MaxValue&&_isSetRange)
  180. {
  181. txtInput.Text = MaxValue.ToString();
  182. Value = MaxValue;
  183. }
  184. else if(tmp<MinValue&&_isSetRange)
  185. {
  186. txtInput.Text = MinValue.ToString();
  187. Value = MinValue;
  188. }
  189. }
  190. }
  191. }