EditTextBlock.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Documents;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. namespace OpenSEMI.Ctrlib.Controls
  7. {
  8. public class EditTextBlock : TextBlock
  9. {
  10. public static readonly DependencyProperty IsInEditModeProperty;
  11. public static readonly DependencyProperty EditBoxModeProperty;
  12. private bool m_AllowEmpty = true;
  13. private bool m_AllowBackgroundChange = false;
  14. public static readonly DependencyProperty MaxValueProperty;
  15. public static readonly DependencyProperty MinValueProperty;
  16. public static readonly DependencyProperty AccuracyProperty;
  17. public static readonly DependencyProperty TextSavedProperty;
  18. public static readonly DependencyProperty ScrollToEndProperty;
  19. private EditTextBlockAdorner _adorner;
  20. public bool IsInEditMode
  21. {
  22. get
  23. {
  24. return (bool)GetValue(IsInEditModeProperty);
  25. }
  26. set
  27. {
  28. SetValue(IsInEditModeProperty, value);
  29. }
  30. }
  31. public EditBoxMode EditBoxMode
  32. {
  33. get
  34. {
  35. return (EditBoxMode)GetValue(EditBoxModeProperty);
  36. }
  37. set
  38. {
  39. SetValue(EditBoxModeProperty, value);
  40. }
  41. }
  42. public bool AllowEmpty
  43. {
  44. get
  45. {
  46. return m_AllowEmpty;
  47. }
  48. set
  49. {
  50. m_AllowEmpty = value;
  51. }
  52. }
  53. public bool AllowBackgroundChange
  54. {
  55. get
  56. {
  57. return m_AllowBackgroundChange;
  58. }
  59. set
  60. {
  61. m_AllowBackgroundChange = value;
  62. }
  63. }
  64. public double MaxValue
  65. {
  66. get
  67. {
  68. return (double)GetValue(MaxValueProperty);
  69. }
  70. set
  71. {
  72. SetValue(MaxValueProperty, value);
  73. }
  74. }
  75. public double MinValue
  76. {
  77. get
  78. {
  79. return (double)GetValue(MinValueProperty);
  80. }
  81. set
  82. {
  83. SetValue(MinValueProperty, value);
  84. }
  85. }
  86. public int Accuracy
  87. {
  88. get
  89. {
  90. return (int)GetValue(AccuracyProperty);
  91. }
  92. set
  93. {
  94. SetValue(AccuracyProperty, value);
  95. }
  96. }
  97. public bool TextSaved
  98. {
  99. get
  100. {
  101. return (bool)GetValue(TextSavedProperty);
  102. }
  103. set
  104. {
  105. SetValue(TextSavedProperty, value);
  106. }
  107. }
  108. public bool IsScrollToEnd
  109. {
  110. get
  111. {
  112. return (bool)GetValue(ScrollToEndProperty);
  113. }
  114. set
  115. {
  116. SetValue(ScrollToEndProperty, value);
  117. }
  118. }
  119. static EditTextBlock()
  120. {
  121. IsInEditModeProperty = DependencyProperty.Register("IsInEditMode", typeof(bool), typeof(EditTextBlock), new UIPropertyMetadata(false, IsInEditModeUpdate));
  122. EditBoxModeProperty = DependencyProperty.Register("EditBoxMode", typeof(EditBoxMode), typeof(EditTextBlock), new UIPropertyMetadata(EditBoxMode.Default));
  123. MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(EditTextBlock), new UIPropertyMetadata(double.NaN));
  124. MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(EditTextBlock), new UIPropertyMetadata(double.NaN));
  125. AccuracyProperty = DependencyProperty.Register("Accuracy", typeof(int), typeof(EditTextBlock), new UIPropertyMetadata(4));
  126. TextSavedProperty = DependencyProperty.Register("TextSaved", typeof(bool), typeof(EditTextBlock), new UIPropertyMetadata(true, TextSavedChangedCallBack));
  127. ScrollToEndProperty = DependencyProperty.Register("IsScrollToEnd", typeof(bool), typeof(EditTextBlock), new UIPropertyMetadata(false));
  128. FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(EditTextBlock), new FrameworkPropertyMetadata(typeof(EditTextBlock)));
  129. }
  130. private static void IsInEditModeUpdate(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  131. {
  132. EditTextBlock editTextBlock = obj as EditTextBlock;
  133. if (editTextBlock != null)
  134. {
  135. AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(editTextBlock);
  136. if (adornerLayer != null)
  137. {
  138. if (editTextBlock.IsInEditMode)
  139. {
  140. if (editTextBlock._adorner == null)
  141. {
  142. editTextBlock._adorner = new EditTextBlockAdorner(editTextBlock);
  143. editTextBlock._adorner.TextBoxKeyUp += editTextBlock.TextBoxKeyUp;
  144. editTextBlock._adorner.TextBoxLostFocus += editTextBlock.TextBoxLostFocus;
  145. }
  146. adornerLayer.Add(editTextBlock._adorner);
  147. }
  148. else
  149. {
  150. Adorner[] adorners = adornerLayer.GetAdorners(editTextBlock);
  151. if (adorners != null)
  152. {
  153. Adorner[] array = adorners;
  154. foreach (Adorner adorner in array)
  155. {
  156. if (adorner is EditTextBlockAdorner)
  157. {
  158. adornerLayer.Remove(adorner);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }
  166. private void TextBoxLostFocus(object sender, RoutedEventArgs e)
  167. {
  168. IsInEditMode = false;
  169. TextBoxEx textBoxEx = sender as TextBoxEx;
  170. if (textBoxEx != null && (EditBoxMode == EditBoxMode.Decimal || EditBoxMode == EditBoxMode.UnSignDecimal || EditBoxMode == EditBoxMode.SignInteger || EditBoxMode == EditBoxMode.UnSignInteger))
  171. {
  172. double result = 0.0;
  173. if (double.TryParse(textBoxEx.Text, out result) && (MinValue != MaxValue || MinValue != 0.0))
  174. {
  175. base.Background = ((result < MinValue || result > MaxValue) ? Brushes.Red : Brushes.Transparent);
  176. }
  177. }
  178. }
  179. private void TextBoxKeyUp(object sender, KeyEventArgs e)
  180. {
  181. if (e.Key == Key.Return)
  182. {
  183. IsInEditMode = false;
  184. TextBoxEx textBoxEx = sender as TextBoxEx;
  185. if (textBoxEx != null && (EditBoxMode == EditBoxMode.Decimal || EditBoxMode == EditBoxMode.UnSignDecimal || EditBoxMode == EditBoxMode.SignInteger || EditBoxMode == EditBoxMode.UnSignInteger))
  186. {
  187. double result = 0.0;
  188. if (double.TryParse(textBoxEx.Text, out result) && (MinValue != MaxValue || MinValue != 0.0))
  189. {
  190. base.Background = ((result < MinValue || result > MaxValue) ? Brushes.Red : Brushes.Transparent);
  191. }
  192. }
  193. }
  194. }
  195. protected override void OnMouseDown(MouseButtonEventArgs e)
  196. {
  197. if (e.MiddleButton == MouseButtonState.Pressed)
  198. {
  199. IsInEditMode = true;
  200. }
  201. else if (e.ClickCount == 1)
  202. {
  203. IsInEditMode = true;
  204. }
  205. }
  206. private static void TextSavedChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args)
  207. {
  208. EditTextBlock editTextBlock = d as EditTextBlock;
  209. if (editTextBlock == null)
  210. {
  211. return;
  212. }
  213. }
  214. }
  215. }