NumberKeyboard.xaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using OpenSEMI.ClientBase;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace MECF.Framework.UI.Client.CenterViews.Dialogs
  19. {
  20. /// <summary>
  21. /// NumberKeyboard.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class NumberKeyboard : Window
  24. {
  25. //public FullKeyboard()
  26. //{
  27. // InitializeComponent();
  28. //}
  29. private String valueString;
  30. private double _maxValue = double.MaxValue;
  31. private double _minValue = double.MinValue;
  32. private bool _isValidate = false;
  33. public String ValueString
  34. {
  35. get
  36. {
  37. ValueHandler(ref valueString);
  38. if (valueString == "")
  39. { return "0"; }
  40. else
  41. {
  42. return valueString;
  43. }
  44. }
  45. }
  46. private int m_KeepDecimals = -1;
  47. public int KeepDecimals
  48. {
  49. get { return m_KeepDecimals; }
  50. set { m_KeepDecimals = value; }
  51. }
  52. public NumberKeyboard(String inputTitle, String inputvalue, Visibility btnHide = Visibility.Visible, bool isValidate = false, double maxValue = double.MaxValue, double minValue = 0)
  53. {
  54. InitializeComponent();
  55. ValueHandler(ref inputvalue);
  56. tbValue.Text = inputvalue;
  57. valueString = inputvalue;
  58. btnMinus.Visibility = btnHide;
  59. btnQuotes.Visibility = btnHide;
  60. _isValidate = isValidate;
  61. _maxValue = maxValue;
  62. _minValue = minValue;
  63. if (btnHide == Visibility.Hidden)
  64. {
  65. btnCancel.SetValue(Grid.RowProperty, 3);
  66. btnClear.SetValue(Grid.RowProperty, 4);
  67. btnCancel.SetValue(Grid.ColumnProperty, 2);
  68. btnClear.SetValue(Grid.ColumnProperty, 0);
  69. }
  70. }
  71. private void ValueHandler(ref string inputvalue)
  72. {
  73. if (!string.IsNullOrEmpty(inputvalue))
  74. {
  75. if (Regex.Matches(inputvalue, ":").Count == 0 && inputvalue.Length > 1)
  76. {
  77. //if(inputvalue.Split('.').Length<=1)
  78. double outValue = 0;
  79. double.TryParse(inputvalue.ToString(), out outValue);
  80. inputvalue = outValue.ToString();
  81. }
  82. }
  83. }
  84. ////通过判断按钮的content属性来做对应处理,以简化大量按钮的编程
  85. private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  86. {
  87. if (e.StylusDevice != null)
  88. {
  89. e.Handled = true;
  90. return;
  91. }
  92. e.Handled = true;
  93. Button clickedButton = (Button)sender; //获取click事件触发源,即按了的按钮
  94. SetButtonValue(clickedButton);
  95. }
  96. private void Button_PreviewTouchDown(object sender, TouchEventArgs e)
  97. //{
  98. // private void ButtonGrid_Click(object sender, RoutedEventArgs e)
  99. {
  100. e.Handled = true;
  101. Button clickedButton = (Button)sender;
  102. // Button clickedButton = (Button)e.OriginalSource; ; //获取click事件触发源,即按了的按钮
  103. SetButtonValue(clickedButton);
  104. }
  105. private void Button_PreviewTouchUp(object sender, TouchEventArgs e)
  106. {
  107. e.Handled = true;
  108. Button clickedButton = (Button)sender;
  109. SetButtonValue(clickedButton);
  110. }
  111. private void Button_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  112. {
  113. if (e.StylusDevice != null)
  114. {
  115. e.Handled = true;
  116. return;
  117. }
  118. e.Handled = true;
  119. Button clickedButton = (Button)sender;
  120. SetButtonValue(clickedButton);
  121. }
  122. private bool IsFristClick = true;
  123. private void SetButtonValue(Button clickedButton)
  124. {
  125. if ((String)clickedButton.Content == "DEL")
  126. {
  127. if (tbValue.Text.Length > 0)
  128. {
  129. tbValue.Text = tbValue.Text.Substring(0, tbValue.Text.Length - 1);
  130. }
  131. }
  132. else if ((String)clickedButton.Content == "Clear")
  133. {
  134. tbValue.Text = "";
  135. }
  136. else if ((String)clickedButton.Content == "Cancel")
  137. {
  138. this.DialogResult = false;
  139. this.Close();
  140. }
  141. else if ((String)clickedButton.Content == "OK")
  142. {
  143. valueString = tbValue.Text;
  144. if (Regex.Matches(valueString, ":").Count > 0)
  145. {
  146. var value = Regex.IsMatch(valueString, "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]");
  147. if (!value)
  148. {
  149. DialogBox.ShowWarning("输入格式不正确");
  150. return;
  151. }
  152. }
  153. double dtempValue;
  154. if (double.TryParse(tbValue.Text, out dtempValue))
  155. {
  156. if (_isValidate)
  157. {
  158. if (dtempValue > _maxValue)
  159. {
  160. DialogBox.ShowWarning($"输入数值超过上限值.{_maxValue}");
  161. return;
  162. }
  163. if (dtempValue < _minValue)
  164. {
  165. DialogBox.ShowWarning($"输入数值低于下限值.{_minValue}");
  166. return;
  167. }
  168. }
  169. }
  170. this.DialogResult = true;
  171. this.Close();
  172. }
  173. else if ((String)clickedButton.Content == "-")
  174. {
  175. if (IsFristClick)
  176. {
  177. IsFristClick = false;
  178. tbValue.Text = "";
  179. }
  180. if (tbValue.Text == "")
  181. {
  182. tbValue.Text += (String)clickedButton.Content;
  183. }
  184. else
  185. {
  186. if (tbValue.Text.Substring(0, 1) == "-")
  187. {
  188. tbValue.Text = tbValue.Text.Substring(1, tbValue.Text.Length - 1);
  189. }
  190. else
  191. {
  192. tbValue.Text = $"-{tbValue.Text }";
  193. }
  194. }
  195. }
  196. else if ((String)clickedButton.Content == "A/a")
  197. {
  198. int count = ButtonGrid.Children.Count;
  199. for (int i = 10; i < count - 4; i++)
  200. {
  201. Button buttonTemp = ButtonGrid.Children[i] as Button;
  202. String contentTemp = buttonTemp.Content as String;
  203. buttonTemp.Content = contentTemp[0] > 90 ? contentTemp.ToUpper() : contentTemp.ToLower();
  204. }
  205. }
  206. else if ((String)clickedButton.Content == ".")
  207. {
  208. if (IsFristClick)
  209. {
  210. IsFristClick = false;
  211. tbValue.Text = "";
  212. }
  213. if (!tbValue.Text.Contains("."))
  214. {
  215. tbValue.Text += (String)clickedButton.Content;
  216. }
  217. }
  218. else
  219. {
  220. if (IsFristClick)
  221. {
  222. IsFristClick = false;
  223. tbValue.Text = "";
  224. }
  225. tbValue.Text += (String)clickedButton.Content;
  226. if (KeepDecimals != -1 && tbValue.Text.Contains("."))
  227. {
  228. int index = tbValue.Text.IndexOf(".");
  229. if (index != -1)
  230. {
  231. string substring = tbValue.Text.Substring(index + 1);
  232. if (substring.Length > KeepDecimals)
  233. {
  234. tbValue.Text = tbValue.Text.Substring(0, tbValue.Text.Length - (substring.Length - KeepDecimals));
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }