SKTextBox.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using SKMC.UI.UserControls;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. namespace SKMC.UI.CustomControls
  11. {
  12. public class SKTextBox : TextBox
  13. {
  14. static SKTextBox()
  15. {
  16. DefaultStyleKeyProperty.OverrideMetadata(typeof(SKTextBox), new FrameworkPropertyMetadata(typeof(SKTextBox)));
  17. }
  18. public SKTextBox()
  19. {
  20. this.PreviewMouseDown += SKTextBox_PreviewMouseDown;
  21. //this.GotFocus += SKTextBox_GotFocus;
  22. }
  23. public string Label
  24. {
  25. get { return (string)GetValue(LabelProperty); }
  26. set { SetValue(LabelProperty, value); }
  27. }
  28. /// <summary>
  29. /// 单位
  30. /// </summary>
  31. public string Unit
  32. {
  33. get { return (string)GetValue(UnitProperty); }
  34. set { SetValue(UnitProperty, value); }
  35. }
  36. /// <summary>
  37. /// 单位字体大小
  38. /// </summary>
  39. public double UnitSize
  40. {
  41. get { return (double)GetValue(UnitSizeProperty); }
  42. set { SetValue(UnitSizeProperty, value); }
  43. }
  44. /// <summary>
  45. /// 值是否有效
  46. /// </summary>
  47. public bool IsValueValid
  48. {
  49. get { return (bool)GetValue(IsValueValidProperty); }
  50. set { SetValue(IsValueValidProperty, value); }
  51. }
  52. /// <summary>
  53. /// 最大值
  54. /// </summary>
  55. public object MaxValue
  56. {
  57. get { return (object)GetValue(MaxValueProperty); }
  58. set { SetValue(MaxValueProperty, value); }
  59. }
  60. /// <summary>
  61. /// 最小值
  62. /// </summary>
  63. public object MinValue
  64. {
  65. get { return (object)GetValue(MinValueProperty); }
  66. set { SetValue(MinValueProperty, value); }
  67. }
  68. /// <summary>
  69. /// 圆角
  70. /// </summary>
  71. public CornerRadius CornerRadius
  72. {
  73. get { return (CornerRadius)GetValue(CornerRadiusProperty); }
  74. set { SetValue(CornerRadiusProperty, value); }
  75. }
  76. public string ToolTipComment
  77. {
  78. get { return (string)GetValue(ToolTipCommentProperty); }
  79. set { SetValue(ToolTipCommentProperty, value); }
  80. }
  81. /// <summary>
  82. /// 图标
  83. /// </summary>
  84. public Geometry Icon
  85. {
  86. get { return (Geometry)GetValue(IconProperty); }
  87. set { SetValue(IconProperty, value); }
  88. }
  89. public double IconSize
  90. {
  91. get { return (double)GetValue(IconSizeProperty); }
  92. set { SetValue(IconSizeProperty, value); }
  93. }
  94. /// <summary>
  95. /// 数字键盘激活
  96. /// </summary>
  97. public bool NumpadActivity
  98. {
  99. get { return (bool)GetValue(NumpadActivityProperty); }
  100. set { SetValue(NumpadActivityProperty, value); }
  101. }
  102. public static readonly DependencyProperty NumpadActivityProperty =
  103. DependencyProperty.Register(nameof(NumpadActivity), typeof(bool), typeof(SKTextBox), new PropertyMetadata(false));
  104. public static readonly DependencyProperty IconProperty =
  105. DependencyProperty.Register("Icon", typeof(Geometry), typeof(SKTextBox), new PropertyMetadata(null));
  106. public static readonly DependencyProperty ToolTipCommentProperty =
  107. DependencyProperty.Register("ToolTipComment", typeof(string), typeof(SKTextBox), new PropertyMetadata(null));
  108. public static readonly DependencyProperty CornerRadiusProperty =
  109. DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(SKTextBox), new PropertyMetadata(new CornerRadius(0)));
  110. public static readonly DependencyProperty MinValueProperty =
  111. DependencyProperty.Register("MinValue", typeof(object), typeof(SKTextBox), new PropertyMetadata(null));
  112. public static readonly DependencyProperty MaxValueProperty =
  113. DependencyProperty.Register("MaxValue", typeof(object), typeof(SKTextBox), new PropertyMetadata(null));
  114. public static readonly DependencyProperty IsValueValidProperty =
  115. DependencyProperty.Register("IsValueValid", typeof(bool), typeof(SKTextBox), new PropertyMetadata(true));
  116. public static readonly DependencyProperty UnitProperty =
  117. DependencyProperty.Register("Unit", typeof(string), typeof(SKTextBox), new PropertyMetadata(""));
  118. public static readonly DependencyProperty LabelProperty =
  119. DependencyProperty.Register("Label", typeof(string), typeof(SKTextBox), new PropertyMetadata(""));
  120. public static readonly DependencyProperty UnitSizeProperty =
  121. DependencyProperty.Register("UnitSize", typeof(double), typeof(SKTextBox), new PropertyMetadata(10.0));
  122. public static readonly DependencyProperty IconSizeProperty =
  123. DependencyProperty.Register(nameof(IconSize), typeof(double), typeof(SKTextBox), new PropertyMetadata(10.0));
  124. /// <summary>
  125. /// 鼠标点击时激活键盘
  126. /// </summary>
  127. /// <param name="sender"></param>
  128. /// <param name="e"></param>
  129. private void SKTextBox_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  130. {
  131. if (!NumpadActivity) return;
  132. if (sender is SKTextBox textBox)
  133. {
  134. // 打开数字键盘
  135. OpenNumpad(textBox);
  136. // 防止默认的焦点行为
  137. e.Handled = true;
  138. }
  139. }
  140. // 获取焦点时激活键盘(作为备选)
  141. //private void SKTextBox_GotFocus(object sender, RoutedEventArgs e)
  142. //{
  143. // if (sender is SKTextBox textBox)
  144. // {
  145. // OpenNumpad(textBox);
  146. // }
  147. //}
  148. // 打开数字键盘
  149. private void OpenNumpad(SKTextBox textBox)
  150. {
  151. if (!NumpadActivity) return;
  152. try
  153. {
  154. // 创建键盘窗口
  155. NumpadControl numpadWindow = new NumpadControl(textBox);
  156. // 设置窗口位置在父窗口中央
  157. numpadWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  158. // 显示对话框
  159. bool? result = numpadWindow.ShowDialog();
  160. // 如果确认了输入,更新文本框
  161. if (result == true && numpadWindow.IsConfirmed)
  162. {
  163. textBox.Text = numpadWindow.InputResult;
  164. // 更新光标位置
  165. textBox.CaretIndex = textBox.Text.Length;
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. MessageBox.Show($"打开键盘失败:{ex.Message}", "错误",
  171. MessageBoxButton.OK, MessageBoxImage.Error);
  172. }
  173. }
  174. }
  175. }