ComboTextBlock.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using System.Collections;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Data;
  6. using System.Windows.Documents;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. namespace OpenSEMI.Ctrlib.Controls
  10. {
  11. public class ComboTextBlock : TextBlock
  12. {
  13. public EditBoxMode EditBoxMode
  14. {
  15. get { return (EditBoxMode)GetValue(EditBoxModeProperty); }
  16. set { SetValue(EditBoxModeProperty, value); }
  17. }
  18. public static readonly DependencyProperty EditBoxModeProperty = DependencyProperty.Register("EditBoxMode", typeof(EditBoxMode), typeof(ComboTextBlock), new UIPropertyMetadata(EditBoxMode.Default));
  19. private bool m_AllowEmpty = true;
  20. public bool AllowEmpty
  21. {
  22. get { return m_AllowEmpty; }
  23. set { m_AllowEmpty = value; }
  24. }
  25. /// <summary>
  26. /// This is a flag to indicate that whether to change the bg color
  27. /// when the text changed by backgroud not by GUI side.
  28. /// This flag is true when control is bound to display text from dialog
  29. /// </summary>
  30. private bool m_AllowBackgroundChange = false;
  31. public bool AllowBackgroundChange
  32. {
  33. get { return m_AllowBackgroundChange; }
  34. set { m_AllowBackgroundChange = value; }
  35. }
  36. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(ComboTextBlock), new UIPropertyMetadata(double.NaN));
  37. public double MaxValue
  38. {
  39. get { return (double)GetValue(MaxValueProperty); }
  40. set { SetValue(MaxValueProperty, value); }
  41. }
  42. public double MinValue
  43. {
  44. get { return (double)GetValue(MinValueProperty); }
  45. set { SetValue(MinValueProperty, value); }
  46. }
  47. public static readonly DependencyProperty MinValueProperty =
  48. DependencyProperty.Register("MinValue", typeof(double), typeof(ComboTextBlock), new UIPropertyMetadata(double.NaN));
  49. public Int32 Accuracy
  50. {
  51. get { return (Int32)GetValue(AccuracyProperty); }
  52. set { SetValue(AccuracyProperty, value); }
  53. }
  54. public static readonly DependencyProperty AccuracyProperty =
  55. DependencyProperty.Register("Accuracy", typeof(Int32), typeof(ComboTextBlock), new UIPropertyMetadata(4));
  56. public bool TextSaved
  57. {
  58. get { return (bool)GetValue(TextSavedProperty); }
  59. set { SetValue(TextSavedProperty, value); }
  60. }
  61. public static readonly DependencyProperty TextSavedProperty =
  62. DependencyProperty.Register("TextSaved", typeof(bool), typeof(ComboTextBlock),
  63. new UIPropertyMetadata(true, new PropertyChangedCallback(TextSavedChangedCallBack)));
  64. public Boolean IsScrollToEnd
  65. {
  66. get { return (Boolean)GetValue(ScrollToEndProperty); }
  67. set { SetValue(ScrollToEndProperty, value); }
  68. }
  69. public static readonly DependencyProperty ScrollToEndProperty =
  70. DependencyProperty.Register("IsScrollToEnd", typeof(Boolean), typeof(ComboTextBlock), new UIPropertyMetadata(false));
  71. private static void TextSavedChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args)
  72. {
  73. ComboTextBlock m_txt = d as ComboTextBlock;
  74. if (m_txt != null)
  75. {
  76. //SetBGColor(m_txt);
  77. }
  78. }
  79. public static readonly DependencyProperty ItemsSourceProperty =
  80. DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ComboTextBlock), new UIPropertyMetadata(null));
  81. public IEnumerable ItemsSource
  82. {
  83. get
  84. {
  85. return (IEnumerable)GetValue(ItemsSourceProperty);
  86. }
  87. set
  88. {
  89. SetValue(ItemsSourceProperty, value);
  90. }
  91. }
  92. public static readonly DependencyProperty SelectedItemProperty =
  93. DependencyProperty.Register("SelectedItem", typeof(object), typeof(ComboTextBlock), new UIPropertyMetadata(null));
  94. public object SelectedItem
  95. {
  96. get
  97. {
  98. return (object)GetValue(SelectedItemProperty);
  99. }
  100. set
  101. {
  102. SetValue(SelectedItemProperty, value);
  103. }
  104. }
  105. public static readonly DependencyProperty IsInEditModeProperty =
  106. DependencyProperty.Register("IsInEditMode", typeof(bool), typeof(ComboTextBlock), new UIPropertyMetadata(false, IsInEditModeUpdate));
  107. public bool IsInEditMode
  108. {
  109. get
  110. {
  111. return (bool)GetValue(IsInEditModeProperty);
  112. }
  113. set
  114. {
  115. SetValue(IsInEditModeProperty, value);
  116. }
  117. }
  118. public bool IsEditable
  119. {
  120. get { return (bool)GetValue(IsEditableProperty); }
  121. set { SetValue(IsEditableProperty, value); }
  122. }
  123. public static readonly DependencyProperty IsEditableProperty =
  124. DependencyProperty.Register("IsEditable", typeof(bool), typeof(ComboTextBlock),
  125. new UIPropertyMetadata(false));
  126. public bool IsLoopItem
  127. {
  128. get { return (bool)GetValue(IsLoopItemProperty); }
  129. set { SetValue(IsLoopItemProperty, value); }
  130. }
  131. public static readonly DependencyProperty IsLoopItemProperty =
  132. DependencyProperty.Register("IsLoopItem", typeof(bool), typeof(ComboTextBlock),
  133. new UIPropertyMetadata(false));
  134. private ComboTextBlockAdorner _adorner;
  135. static ComboTextBlock()
  136. {
  137. DefaultStyleKeyProperty.OverrideMetadata(typeof(ComboTextBlock), new FrameworkPropertyMetadata(typeof(ComboTextBlock)));
  138. }
  139. private static void IsInEditModeUpdate(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  140. {
  141. ComboTextBlock textBlock = obj as ComboTextBlock;
  142. if (null != textBlock)
  143. {
  144. //Get the adorner layer of the uielement (here TextBlock)
  145. AdornerLayer layer = AdornerLayer.GetAdornerLayer(textBlock);
  146. if (layer == null)
  147. return;
  148. //If the IsInEditMode set to true means the user has enabled the edit mode then
  149. //add the adorner to the adorner layer of the TextBlock.
  150. if (textBlock.IsInEditMode)
  151. {
  152. if (null == textBlock._adorner)
  153. {
  154. textBlock._adorner = new ComboTextBlockAdorner(textBlock);
  155. //Events wired to exit edit mode when the user presses Enter key or leaves the control.
  156. //textBlock._adorner.TextBoxKeyUp += textBlock.TextBoxKeyUp;
  157. //textBlock._adorner.TextBoxLostFocus += textBlock.TextBoxLostFocus;
  158. }
  159. layer.Add(textBlock._adorner);
  160. }
  161. else
  162. {
  163. //Remove the adorner from the adorner layer.
  164. Adorner[] adorners = layer.GetAdorners(textBlock);
  165. if (adorners != null)
  166. {
  167. foreach (Adorner adorner in adorners)
  168. {
  169. if (adorner is ComboTextBlockAdorner)
  170. {
  171. layer.Remove(adorner);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. private void LeaveEditMode()
  179. {
  180. IsInEditMode = false;
  181. //if (!IsLoopItem)
  182. // Background = Brushes.Transparent;
  183. }
  184. public void TextBoxLostFocus(object sender, RoutedEventArgs e)
  185. {
  186. LeaveEditMode();
  187. }
  188. internal void DropDownClosedLostFocus(object sender, EventArgs e)
  189. {
  190. LeaveEditMode();
  191. }
  192. /// <summary>
  193. /// release the edit mode when user presses enter.
  194. /// </summary>
  195. /// <param name="sender">The sender.</param>
  196. /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
  197. private void TextBoxKeyUp(object sender, KeyEventArgs e)
  198. {
  199. if (e.Key == Key.Enter)
  200. {
  201. IsInEditMode = false;
  202. //TextBoxEx atb = sender as TextBoxEx;
  203. //if (atb == null)
  204. // return;
  205. //if (this.EditBoxMode == EditBoxMode.Decimal || this.EditBoxMode == EditBoxMode.UnSignDecimal ||
  206. // this.EditBoxMode == EditBoxMode.SignInteger || this.EditBoxMode == EditBoxMode.UnSignInteger)
  207. //{
  208. // double dataValue = 0;
  209. // if (!double.TryParse(atb.Text, out dataValue))
  210. // return;
  211. // if (MinValue == MaxValue && MinValue == 0)
  212. // return;
  213. // this.Background = (dataValue < MinValue || dataValue > MaxValue) ? Brushes.Red : Brushes.Transparent;
  214. //}
  215. }
  216. }
  217. /// <summary>
  218. /// 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.
  219. /// </summary>
  220. /// <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>
  221. protected override void OnMouseDown(MouseButtonEventArgs e)
  222. {
  223. if (e.MiddleButton == MouseButtonState.Pressed)
  224. {
  225. IsInEditMode = true;
  226. }
  227. else if (e.ClickCount == 1)
  228. {
  229. IsInEditMode = true;
  230. }
  231. }
  232. }
  233. }