EditTextBlock.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Data;
  5. using System.Windows.Documents;
  6. using System.Windows.Input;
  7. using System.Windows.Media;
  8. namespace OpenSEMI.Ctrlib.Controls
  9. {
  10. public class EditTextBlock : TextBlock
  11. {
  12. static EditTextBlock()
  13. {
  14. DefaultStyleKeyProperty.OverrideMetadata(typeof(EditTextBlock), new FrameworkPropertyMetadata(typeof(EditTextBlock)));
  15. }
  16. public static readonly DependencyProperty IsInEditModeProperty =
  17. DependencyProperty.Register("IsInEditMode", typeof(bool), typeof(EditTextBlock), new UIPropertyMetadata(false, IsInEditModeUpdate));
  18. public bool IsInEditMode
  19. {
  20. get
  21. {
  22. return (bool)GetValue(IsInEditModeProperty);
  23. }
  24. set
  25. {
  26. SetValue(IsInEditModeProperty, value);
  27. }
  28. }
  29. private static void IsInEditModeUpdate(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  30. {
  31. EditTextBlock textBlock = obj as EditTextBlock;
  32. if (null != textBlock)
  33. {
  34. //Get the adorner layer of the uielement (here TextBlock)
  35. AdornerLayer layer = AdornerLayer.GetAdornerLayer(textBlock);
  36. if (layer == null)
  37. return;
  38. //If the IsInEditMode set to true means the user has enabled the edit mode then
  39. //add the adorner to the adorner layer of the TextBlock.
  40. if (textBlock.IsInEditMode)
  41. {
  42. if (null == textBlock._adorner)
  43. {
  44. textBlock._adorner = new EditTextBlockAdorner(textBlock);
  45. //Events wired to exit edit mode when the user presses Enter key or leaves the control.
  46. textBlock._adorner.TextBoxKeyUp += textBlock.TextBoxKeyUp;
  47. textBlock._adorner.TextBoxLostFocus += textBlock.TextBoxLostFocus;
  48. textBlock._adorner.TextBox.SelectAll();
  49. }
  50. layer.Add(textBlock._adorner);
  51. }
  52. else
  53. {
  54. //Remove the adorner from the adorner layer.
  55. Adorner[] adorners = layer.GetAdorners(textBlock);
  56. if (adorners != null)
  57. {
  58. foreach (Adorner adorner in adorners)
  59. {
  60. if (adorner is EditTextBlockAdorner)
  61. {
  62. layer.Remove(adorner);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. private void TextBoxLostFocus(object sender, RoutedEventArgs e)
  70. {
  71. IsInEditMode = false;
  72. TextBoxEx atb = sender as TextBoxEx;
  73. if (atb == null)
  74. return;
  75. if (this.EditBoxMode == EditBoxMode.Decimal || this.EditBoxMode == EditBoxMode.UnSignDecimal ||
  76. this.EditBoxMode == EditBoxMode.SignInteger || this.EditBoxMode == EditBoxMode.UnSignInteger)
  77. {
  78. double dataValue = 0;
  79. if (!double.TryParse(atb.Text, out dataValue))
  80. return;
  81. if (MinValue == MaxValue && MinValue == 0)
  82. return;
  83. this.Background = (dataValue < MinValue || dataValue > MaxValue) ? Brushes.Red : Brushes.Transparent;
  84. }
  85. }
  86. /// <summary>
  87. /// release the edit mode when user presses enter.
  88. /// </summary>
  89. /// <param name="sender">The sender.</param>
  90. /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
  91. private void TextBoxKeyUp(object sender, KeyEventArgs e)
  92. {
  93. if (e.Key == Key.Enter)
  94. {
  95. IsInEditMode = false;
  96. TextBoxEx atb = sender as TextBoxEx;
  97. if (atb == null)
  98. return;
  99. if (this.EditBoxMode == EditBoxMode.Decimal || this.EditBoxMode == EditBoxMode.UnSignDecimal ||
  100. this.EditBoxMode == EditBoxMode.SignInteger || this.EditBoxMode == EditBoxMode.UnSignInteger)
  101. {
  102. double dataValue = 0;
  103. if (!double.TryParse(atb.Text, out dataValue))
  104. return;
  105. if (MinValue == MaxValue && MinValue == 0)
  106. return;
  107. this.Background = (dataValue < MinValue || dataValue > MaxValue) ? Brushes.Red : Brushes.Transparent;
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.MouseDown"/> attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
  113. /// </summary>
  114. /// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs"/> that contains the event data. This event data reports details about the mouse button that was pressed and the handled state.</param>
  115. protected override void OnMouseDown(MouseButtonEventArgs e)
  116. {
  117. if (e.MiddleButton == MouseButtonState.Pressed)
  118. {
  119. IsInEditMode = true;
  120. }
  121. else if (e.ClickCount == 1)
  122. {
  123. IsInEditMode = true;
  124. }
  125. }
  126. #region EditTextBox properties
  127. public EditBoxMode EditBoxMode
  128. {
  129. get { return (EditBoxMode)GetValue(EditBoxModeProperty); }
  130. set { SetValue(EditBoxModeProperty, value); }
  131. }
  132. public static readonly DependencyProperty EditBoxModeProperty = DependencyProperty.Register("EditBoxMode", typeof(EditBoxMode), typeof(EditTextBlock), new UIPropertyMetadata(EditBoxMode.Default));
  133. private bool m_AllowEmpty = true;
  134. public bool AllowEmpty
  135. {
  136. get { return m_AllowEmpty; }
  137. set { m_AllowEmpty = value; }
  138. }
  139. /// <summary>
  140. /// This is a flag to indicate that whether to change the bg color
  141. /// when the text changed by backgroud not by GUI side.
  142. /// This flag is true when control is bound to display text from dialog
  143. /// </summary>
  144. private bool m_AllowBackgroundChange = false;
  145. public bool AllowBackgroundChange
  146. {
  147. get { return m_AllowBackgroundChange; }
  148. set { m_AllowBackgroundChange = value; }
  149. }
  150. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(EditTextBlock), new UIPropertyMetadata(double.NaN));
  151. public double MaxValue
  152. {
  153. get { return (double)GetValue(MaxValueProperty); }
  154. set { SetValue(MaxValueProperty, value); }
  155. }
  156. public double MinValue
  157. {
  158. get { return (double)GetValue(MinValueProperty); }
  159. set { SetValue(MinValueProperty, value); }
  160. }
  161. public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(EditTextBlock), new UIPropertyMetadata(double.NaN));
  162. public Int32 Accuracy
  163. {
  164. get { return (Int32)GetValue(AccuracyProperty); }
  165. set { SetValue(AccuracyProperty, value); }
  166. }
  167. public static readonly DependencyProperty AccuracyProperty = DependencyProperty.Register("Accuracy", typeof(Int32), typeof(EditTextBlock), new UIPropertyMetadata(4));
  168. public bool TextSaved
  169. {
  170. get { return (bool)GetValue(TextSavedProperty); }
  171. set { SetValue(TextSavedProperty, value); }
  172. }
  173. public static readonly DependencyProperty TextSavedProperty =
  174. DependencyProperty.Register("TextSaved", typeof(bool), typeof(EditTextBlock),
  175. new UIPropertyMetadata(true, new PropertyChangedCallback(TextSavedChangedCallBack)));
  176. public Boolean IsScrollToEnd
  177. {
  178. get { return (Boolean)GetValue(ScrollToEndProperty); }
  179. set { SetValue(ScrollToEndProperty, value); }
  180. }
  181. public static readonly DependencyProperty ScrollToEndProperty = DependencyProperty.Register("IsScrollToEnd", typeof(Boolean), typeof(EditTextBlock), new UIPropertyMetadata(false));
  182. private static void TextSavedChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args)
  183. {
  184. EditTextBlock m_txt = d as EditTextBlock;
  185. if (m_txt != null)
  186. {
  187. //SetBGColor(m_txt);
  188. }
  189. }
  190. #endregion
  191. private EditTextBlockAdorner _adorner;
  192. }
  193. }