NumberKeyboard.xaml.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. private void Window_Deactivated(object sender, EventArgs e)
  85. {
  86. try
  87. {
  88. this.DialogResult = false;
  89. this.Close();
  90. }
  91. catch (Exception)
  92. {
  93. }
  94. // Close the window when it loses focus
  95. }
  96. ////通过判断按钮的content属性来做对应处理,以简化大量按钮的编程
  97. private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  98. {
  99. if (e.StylusDevice != null)
  100. {
  101. e.Handled = true;
  102. return;
  103. }
  104. e.Handled = true;
  105. Button clickedButton = (Button)sender; //获取click事件触发源,即按了的按钮
  106. SetButtonValue(clickedButton);
  107. }
  108. private void Button_PreviewTouchDown(object sender, TouchEventArgs e)
  109. //{
  110. // private void ButtonGrid_Click(object sender, RoutedEventArgs e)
  111. {
  112. e.Handled = true;
  113. Button clickedButton = (Button)sender;
  114. // Button clickedButton = (Button)e.OriginalSource; ; //获取click事件触发源,即按了的按钮
  115. SetButtonValue(clickedButton);
  116. }
  117. private void Button_PreviewTouchUp(object sender, TouchEventArgs e)
  118. {
  119. e.Handled = true;
  120. Button clickedButton = (Button)sender;
  121. SetButtonValue(clickedButton);
  122. }
  123. private void Button_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  124. {
  125. if (e.StylusDevice != null)
  126. {
  127. e.Handled = true;
  128. return;
  129. }
  130. e.Handled = true;
  131. Button clickedButton = (Button)sender;
  132. SetButtonValue(clickedButton);
  133. }
  134. private bool IsFristClick = true;
  135. private void SetButtonValue(Button clickedButton)
  136. {
  137. if ((String)clickedButton.Content == "DEL")
  138. {
  139. if (tbValue.Text.Length > 0)
  140. {
  141. tbValue.Text = tbValue.Text.Substring(0, tbValue.Text.Length - 1);
  142. }
  143. }
  144. else if ((String)clickedButton.Content == "Clear")
  145. {
  146. tbValue.Text = "";
  147. }
  148. else if ((String)clickedButton.Content == "Cancel")
  149. {
  150. this.DialogResult = false;
  151. this.Close();
  152. }
  153. else if ((String)clickedButton.Content == "OK")
  154. {
  155. valueString = tbValue.Text;
  156. if (Regex.Matches(valueString, ":").Count > 0)
  157. {
  158. var value = Regex.IsMatch(valueString, "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]");
  159. if (!value)
  160. {
  161. DialogBox.ShowWarning("输入格式不正确");
  162. return;
  163. }
  164. }
  165. double dtempValue;
  166. if (double.TryParse(tbValue.Text, out dtempValue))
  167. {
  168. if (_isValidate)
  169. {
  170. if (dtempValue > _maxValue)
  171. {
  172. DialogBox.ShowWarning($"输入数值超过上限值.{_maxValue}");
  173. return;
  174. }
  175. if (dtempValue < _minValue)
  176. {
  177. DialogBox.ShowWarning($"输入数值低于下限值.{_minValue}");
  178. return;
  179. }
  180. }
  181. }
  182. this.DialogResult = true;
  183. this.Close();
  184. }
  185. else if ((String)clickedButton.Content == "-")
  186. {
  187. if (IsFristClick)
  188. {
  189. IsFristClick = false;
  190. tbValue.Text = "";
  191. }
  192. if (tbValue.Text == "")
  193. {
  194. tbValue.Text += (String)clickedButton.Content;
  195. }
  196. else
  197. {
  198. if (tbValue.Text.Substring(0, 1) == "-")
  199. {
  200. tbValue.Text = tbValue.Text.Substring(1, tbValue.Text.Length - 1);
  201. }
  202. else
  203. {
  204. tbValue.Text = $"-{tbValue.Text }";
  205. }
  206. }
  207. }
  208. else if ((String)clickedButton.Content == "A/a")
  209. {
  210. int count = ButtonGrid.Children.Count;
  211. for (int i = 10; i < count - 4; i++)
  212. {
  213. Button buttonTemp = ButtonGrid.Children[i] as Button;
  214. String contentTemp = buttonTemp.Content as String;
  215. buttonTemp.Content = contentTemp[0] > 90 ? contentTemp.ToUpper() : contentTemp.ToLower();
  216. }
  217. }
  218. else if ((String)clickedButton.Content == ".")
  219. {
  220. if (IsFristClick)
  221. {
  222. IsFristClick = false;
  223. tbValue.Text = "";
  224. }
  225. if (!tbValue.Text.Contains("."))
  226. {
  227. tbValue.Text += (String)clickedButton.Content;
  228. }
  229. }
  230. else
  231. {
  232. if (IsFristClick)
  233. {
  234. IsFristClick = false;
  235. tbValue.Text = "";
  236. }
  237. tbValue.Text += (String)clickedButton.Content;
  238. if (KeepDecimals != -1 && tbValue.Text.Contains("."))
  239. {
  240. int index = tbValue.Text.IndexOf(".");
  241. if (index != -1)
  242. {
  243. string substring = tbValue.Text.Substring(index + 1);
  244. if (substring.Length > KeepDecimals)
  245. {
  246. tbValue.Text = tbValue.Text.Substring(0, tbValue.Text.Length - (substring.Length - KeepDecimals));
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }