TextBoxEx.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using Caliburn.Micro;
  2. using MECF.Framework.Common.DataCenter;
  3. using MECF.Framework.UI.Client.CenterViews.Dialogs;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Input;
  12. namespace MECF.Framework.UI.Client.Themes.OceanBlue
  13. {
  14. public partial class TextBoxEx : ResourceDictionary
  15. {
  16. public bool EnablePopupKeyboard { get; set; } = true;
  17. public void OnClick(object sender, MouseButtonEventArgs e)
  18. {
  19. if (e.StylusDevice != null)
  20. {
  21. e.Handled = true;
  22. return;
  23. }
  24. if (sender is TextBox)
  25. {
  26. TextBox textBox = sender as TextBox;
  27. if (textBox.Tag != null && textBox.Tag.ToString().Contains("None"))
  28. {
  29. e.Handled = false;
  30. return;
  31. }
  32. else
  33. {
  34. e.Handled = true;
  35. }
  36. string strRet = Show(sender, textBox.Text);
  37. if (string.IsNullOrEmpty(strRet)) return;
  38. textBox.Text = strRet;
  39. }
  40. else if (sender is PasswordBox)
  41. {
  42. PasswordBox passwordBox = sender as PasswordBox;
  43. string strRet = Show(sender, passwordBox.Password);
  44. if (string.IsNullOrEmpty(strRet)) return;
  45. passwordBox.Password = strRet;
  46. }
  47. }
  48. public void TextBox_PreviewTouchDown(object sender, TouchEventArgs e)
  49. {
  50. if (sender is TextBox)
  51. {
  52. TextBox textBox = sender as TextBox;
  53. string strRet = Show(sender, textBox.Text);
  54. if (string.IsNullOrEmpty(strRet)) return;
  55. textBox.Text = strRet;
  56. }
  57. else if (sender is PasswordBox)
  58. {
  59. PasswordBox passwordBox = sender as PasswordBox;
  60. string strRet = Show(sender, passwordBox.Password);
  61. if (string.IsNullOrEmpty(strRet)) return;
  62. passwordBox.Password = strRet;
  63. }
  64. }
  65. public void OnLoaded(object sender, RoutedEventArgs e)
  66. {
  67. if (sender is TextBox)
  68. {
  69. TextBox textBox = sender as TextBox;
  70. if (QueryDataClient.Instance.Service.GetConfig($"System.EnablePopupKeyboard") != null)
  71. EnablePopupKeyboard = (bool)QueryDataClient.Instance.Service.GetConfig($"System.EnablePopupKeyboard");
  72. textBox.IsReadOnly = EnablePopupKeyboard;
  73. }
  74. }
  75. private string Show(object sender, string strDefaultValue)
  76. {
  77. Control control = sender as Control;
  78. string strRet = string.Empty;
  79. if (QueryDataClient.Instance.Service.GetConfig($"System.EnablePopupKeyboard") != null)
  80. EnablePopupKeyboard = (bool)QueryDataClient.Instance.Service.GetConfig($"System.EnablePopupKeyboard");
  81. if (!EnablePopupKeyboard) { return string.Empty; }
  82. if (control.Tag != null && control.Tag.ToString().Contains("Number"))
  83. {
  84. NumberKeyboard numberKeyboard = new NumberKeyboard("", strDefaultValue);
  85. if (control.Tag.ToString().Contains("SubPanel"))
  86. {
  87. numberKeyboard.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  88. }
  89. else
  90. {
  91. if (control.Tag.ToString().Contains(":"))
  92. {
  93. var subTagArray = control.Tag.ToString().Split(':');
  94. if (subTagArray.Length > 1)
  95. {
  96. int keepDecimals = -1;
  97. if (int.TryParse(subTagArray[1], out keepDecimals))
  98. {
  99. if (keepDecimals != -1) numberKeyboard.KeepDecimals = keepDecimals;
  100. }
  101. }
  102. }
  103. var point = control.PointFromScreen(new Point(0, 0));
  104. double x = SystemParameters.WorkArea.Width;
  105. double y = SystemParameters.WorkArea.Height;
  106. if (-point.Y + control.ActualHeight + 5 + numberKeyboard.Height < y)
  107. {
  108. numberKeyboard.Top = -point.Y + control.ActualHeight + 5;
  109. }
  110. else
  111. {
  112. numberKeyboard.Top = -point.Y - numberKeyboard.Height - 5;
  113. }
  114. if (-point.X + numberKeyboard.Width < x)
  115. {
  116. numberKeyboard.Left = -point.X;
  117. }
  118. else
  119. {
  120. numberKeyboard.Left = -point.X - (numberKeyboard.Width - control.ActualWidth);
  121. }
  122. }
  123. if ((bool)numberKeyboard.ShowDialog()) strRet = numberKeyboard.ValueString;
  124. }
  125. else if (control.Tag != null && control.Tag.ToString().Contains("None"))
  126. {
  127. return string.Empty;
  128. }
  129. else if (control.Tag != null && control.Tag.ToString().Contains("NotClearFull"))
  130. {
  131. FullKeyboard fullKeyboard = new FullKeyboard("", strDefaultValue);
  132. fullKeyboard.IsFristClick = false;
  133. if (control.Tag != null && control.Tag.ToString().Contains("SubPanel"))
  134. {
  135. fullKeyboard.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  136. }
  137. else
  138. {
  139. var point = control.PointFromScreen(new Point(0, 0));
  140. double x = SystemParameters.WorkArea.Width;
  141. double y = SystemParameters.WorkArea.Height;
  142. if (-point.Y + control.ActualHeight + 5 + fullKeyboard.Height < y)
  143. {
  144. fullKeyboard.Top = -point.Y + control.ActualHeight + 5;
  145. }
  146. else
  147. {
  148. fullKeyboard.Top = -point.Y - fullKeyboard.Height - 5;
  149. }
  150. if (-point.X + fullKeyboard.Width < x)
  151. {
  152. fullKeyboard.Left = -point.X;
  153. }
  154. else
  155. {
  156. fullKeyboard.Left = -point.X - (fullKeyboard.Width - control.ActualWidth);
  157. }
  158. }
  159. if ((bool)fullKeyboard.ShowDialog()) strRet = fullKeyboard.ValueString;
  160. return strRet;
  161. }
  162. else
  163. {
  164. FullKeyboard fullKeyboard = new FullKeyboard("", strDefaultValue);
  165. if (control.Tag != null && control.Tag.ToString().Contains("SubPanel"))
  166. {
  167. fullKeyboard.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  168. }
  169. else
  170. {
  171. var point = control.PointFromScreen(new Point(0, 0));
  172. double x = SystemParameters.WorkArea.Width;
  173. double y = SystemParameters.WorkArea.Height;
  174. if (-point.Y + control.ActualHeight + 5 + fullKeyboard.Height < y)
  175. {
  176. fullKeyboard.Top = -point.Y + control.ActualHeight + 5;
  177. }
  178. else
  179. {
  180. fullKeyboard.Top = -point.Y - fullKeyboard.Height - 5;
  181. }
  182. if (-point.X + fullKeyboard.Width < x)
  183. {
  184. fullKeyboard.Left = -point.X;
  185. }
  186. else
  187. {
  188. fullKeyboard.Left = -point.X - (fullKeyboard.Width - control.ActualWidth);
  189. }
  190. }
  191. if ((bool)fullKeyboard.ShowDialog()) strRet = fullKeyboard.ValueString;
  192. }
  193. return strRet;
  194. }
  195. }
  196. }