NumberDigitKeyboard.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. //通过判断按钮的content属性来做对应处理,以简化大量按钮的编程
  78. private void ButtonGrid_Click(object sender, RoutedEventArgs e)
  79. {
  80. Button clickedButton = (Button)e.OriginalSource; //获取click事件触发源,即按了的按钮
  81. if ((String)clickedButton.Content == "DEL")
  82. {
  83. if (tbValue.Text.Length > 0)
  84. {
  85. tbValue.Text = tbValue.Text.Substring(0, tbValue.Text.Length - 1);
  86. }
  87. }
  88. else if ((String)clickedButton.Content == "Clear")
  89. {
  90. tbValue.Text = "";
  91. }
  92. else if ((String)clickedButton.Content == "Cancel")
  93. {
  94. this.DialogResult = false;
  95. this.Close();
  96. }
  97. else if ((String)clickedButton.Content == "OK")
  98. {
  99. valueString = tbValue.Text;
  100. if (Regex.Matches(valueString, ":").Count > 0)
  101. {
  102. var value = Regex.IsMatch(valueString, "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]");
  103. if (!value)
  104. {
  105. DialogBox.ShowWarning("输入格式不正确");
  106. return;
  107. }
  108. }
  109. double dtempValue;
  110. if (double.TryParse(tbValue.Text, out dtempValue))
  111. {
  112. if (_isValidate)
  113. {
  114. if (dtempValue > _maxValue)
  115. {
  116. DialogBox.ShowWarning($"输入数值超过上限值.{_maxValue}");
  117. return;
  118. }
  119. if (dtempValue < _minValue)
  120. {
  121. DialogBox.ShowWarning($"输入数值低于下限值.{_minValue}");
  122. return;
  123. }
  124. }
  125. }
  126. this.DialogResult = true;
  127. this.Close();
  128. }
  129. else if ((String)clickedButton.Content == "-")
  130. {
  131. if (tbValue.Text == "")
  132. {
  133. tbValue.Text += (String)clickedButton.Content;
  134. }
  135. else
  136. {
  137. if (tbValue.Text.Substring(0, 1) == "-")
  138. {
  139. tbValue.Text = tbValue.Text.Substring(1, tbValue.Text.Length - 1);
  140. }
  141. else
  142. {
  143. tbValue.Text = $"-{tbValue.Text }";
  144. }
  145. }
  146. }
  147. else if ((String)clickedButton.Content == "A/a")
  148. {
  149. int count = ButtonGrid.Children.Count;
  150. for (int i = 10; i < count - 4; i++)
  151. {
  152. Button buttonTemp = ButtonGrid.Children[i] as Button;
  153. String contentTemp = buttonTemp.Content as String;
  154. buttonTemp.Content = contentTemp[0] > 90 ? contentTemp.ToUpper() : contentTemp.ToLower();
  155. }
  156. }
  157. else
  158. {
  159. tbValue.Text += (String)clickedButton.Content;
  160. }
  161. }
  162. private void ButtonAddSub_Click(object sender, RoutedEventArgs e)
  163. {
  164. }
  165. }
  166. }