NumberDigitKeyboard.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace MECF.Framework.UI.Client.CenterViews.Dialogs
  18. {
  19. /// <summary>
  20. /// NumberKeyboard.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class NumberDigitKeyboard : Window
  23. {
  24. //public FullKeyboard()
  25. //{
  26. // InitializeComponent();
  27. //}
  28. private String valueString;
  29. private double _maxValue=double.MaxValue;
  30. private double _minValue=double.MinValue;
  31. private bool _isValidate = false;
  32. public String ValueString
  33. {
  34. get
  35. {
  36. ValueHandler(ref valueString);
  37. if (valueString == "")
  38. { return "0"; }
  39. else
  40. {
  41. return valueString;
  42. }
  43. }
  44. }
  45. public NumberDigitKeyboard(String inputTitle, String inputvalue,Visibility btnHide=Visibility.Visible,bool isValidate=false ,double maxValue=double.MaxValue,double minValue=0)
  46. {
  47. InitializeComponent();
  48. ValueHandler(ref inputvalue);
  49. tbValue.Text = inputvalue;
  50. valueString = inputvalue;
  51. btnMinus.Visibility = btnHide;
  52. btnQuotes.Visibility = btnHide;
  53. _isValidate = isValidate;
  54. _maxValue = maxValue;
  55. _minValue = minValue;
  56. if (btnHide == Visibility.Hidden)
  57. {
  58. btnCancel.SetValue(Grid.RowProperty, 3);
  59. btnClear.SetValue(Grid.RowProperty, 4);
  60. btnCancel.SetValue(Grid.ColumnProperty, 2);
  61. btnClear.SetValue(Grid.ColumnProperty, 0);
  62. }
  63. }
  64. private void ValueHandler(ref string inputvalue)
  65. {
  66. if (!string.IsNullOrEmpty(inputvalue))
  67. {
  68. if (Regex.Matches(inputvalue, ":").Count == 0 && inputvalue.Length > 1)
  69. {
  70. //if(inputvalue.Split('.').Length<=1)
  71. double outValue = 0;
  72. double.TryParse(inputvalue.ToString(),out outValue);
  73. inputvalue = outValue.ToString();
  74. }
  75. }
  76. }
  77. private void Window_Deactivated(object sender, EventArgs e)
  78. {
  79. try
  80. {
  81. this.DialogResult = false;
  82. this.Close();
  83. }
  84. catch (Exception)
  85. {
  86. }
  87. // Close the window when it loses focus
  88. }
  89. //通过判断按钮的content属性来做对应处理,以简化大量按钮的编程
  90. private void ButtonGrid_Click(object sender, RoutedEventArgs e)
  91. {
  92. Button clickedButton = (Button)e.OriginalSource; //获取click事件触发源,即按了的按钮
  93. if ((String)clickedButton.Content == "DEL")
  94. {
  95. if (tbValue.Text.Length > 0)
  96. {
  97. tbValue.Text = tbValue.Text.Substring(0, tbValue.Text.Length - 1);
  98. }
  99. }
  100. else if ((String)clickedButton.Content == "Clear")
  101. {
  102. tbValue.Text = "";
  103. }
  104. else if ((String)clickedButton.Content == "Cancel")
  105. {
  106. this.DialogResult = false;
  107. this.Close();
  108. }
  109. else if ((String)clickedButton.Content == "OK")
  110. {
  111. valueString = tbValue.Text;
  112. if (Regex.Matches(valueString, ":").Count > 0)
  113. {
  114. var value = Regex.IsMatch(valueString, "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]");
  115. if (!value)
  116. {
  117. DialogBox.ShowWarning("输入格式不正确");
  118. return;
  119. }
  120. }
  121. double dtempValue;
  122. if (double.TryParse(tbValue.Text, out dtempValue))
  123. {
  124. if (_isValidate)
  125. {
  126. if (dtempValue > _maxValue)
  127. {
  128. DialogBox.ShowWarning($"输入数值超过上限值.{_maxValue}");
  129. return;
  130. }
  131. if (dtempValue < _minValue)
  132. {
  133. DialogBox.ShowWarning($"输入数值低于下限值.{_minValue}");
  134. return;
  135. }
  136. }
  137. }
  138. this.DialogResult = true;
  139. this.Close();
  140. }
  141. else if ((String)clickedButton.Content == "-")
  142. {
  143. if (tbValue.Text == "")
  144. {
  145. tbValue.Text += (String)clickedButton.Content;
  146. }
  147. else
  148. {
  149. if (tbValue.Text.Substring(0, 1) == "-")
  150. {
  151. tbValue.Text = tbValue.Text.Substring(1, tbValue.Text.Length - 1);
  152. }
  153. else
  154. {
  155. tbValue.Text = $"-{tbValue.Text }";
  156. }
  157. }
  158. }
  159. else if ((String)clickedButton.Content == "A/a")
  160. {
  161. int count = ButtonGrid.Children.Count;
  162. for (int i = 10; i < count - 4; i++)
  163. {
  164. Button buttonTemp = ButtonGrid.Children[i] as Button;
  165. String contentTemp = buttonTemp.Content as String;
  166. buttonTemp.Content = contentTemp[0] > 90 ? contentTemp.ToUpper() : contentTemp.ToLower();
  167. }
  168. }
  169. else
  170. {
  171. tbValue.Text += (String)clickedButton.Content;
  172. }
  173. }
  174. private void ButtonAddSub_Click(object sender, RoutedEventArgs e)
  175. {
  176. }
  177. }
  178. }