EditableTextBlock.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Data;
  4. using System.Windows.Documents;
  5. using System.Windows.Input;
  6. namespace GeekDesk.EditTextBlock
  7. {
  8. public class EditableTextBlock : TextBlock
  9. {
  10. public bool IsInEditMode
  11. {
  12. get
  13. {
  14. return (bool)GetValue(IsInEditModeProperty);
  15. }
  16. set
  17. {
  18. SetValue(IsInEditModeProperty, value);
  19. }
  20. }
  21. private EditableTextBlockAdorner _adorner;
  22. // Using a DependencyProperty as the backing store for IsInEditMode. This enables animation, styling, binding, etc...
  23. public static readonly DependencyProperty IsInEditModeProperty =
  24. DependencyProperty.Register("IsInEditMode", typeof(bool), typeof(EditableTextBlock), new UIPropertyMetadata(false, IsInEditModeUpdate));
  25. /// <summary>
  26. /// Determines whether [is in edit mode update] [the specified obj].
  27. /// </summary>
  28. /// <param name="obj">The obj.</param>
  29. /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  30. private static void IsInEditModeUpdate(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  31. {
  32. EditableTextBlock textBlock = obj as EditableTextBlock;
  33. if (null != textBlock)
  34. {
  35. //Get the adorner layer of the uielement (here TextBlock)
  36. AdornerLayer layer = AdornerLayer.GetAdornerLayer(textBlock);
  37. //If the IsInEditMode set to true means the user has enabled the edit mode then
  38. //add the adorner to the adorner layer of the TextBlock.
  39. if (textBlock.IsInEditMode)
  40. {
  41. if (null == textBlock._adorner)
  42. {
  43. textBlock._adorner = new EditableTextBlockAdorner(textBlock);
  44. //Events wired to exit edit mode when the user presses Enter key or leaves the control.
  45. textBlock._adorner.TextBoxKeyUp += textBlock.TextBoxKeyUp;
  46. textBlock._adorner.TextBoxLostFocus += textBlock.TextBoxLostFocus;
  47. }
  48. layer.Add(textBlock._adorner);
  49. }
  50. else
  51. {
  52. //Remove the adorner from the adorner layer.
  53. Adorner[] adorners = layer.GetAdorners(textBlock);
  54. if (adorners != null)
  55. {
  56. foreach (Adorner adorner in adorners)
  57. {
  58. if (adorner is EditableTextBlockAdorner)
  59. {
  60. layer.Remove(adorner);
  61. }
  62. }
  63. }
  64. //Update the textblock's text binding.
  65. BindingExpression expression = textBlock.GetBindingExpression(TextProperty);
  66. if (null != expression)
  67. {
  68. expression.UpdateTarget();
  69. }
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// Gets or sets the length of the max.
  75. /// </summary>
  76. /// <value>The length of the max.</value>
  77. public int MaxLength
  78. {
  79. get
  80. {
  81. return (int)GetValue(MaxLengthProperty);
  82. }
  83. set
  84. {
  85. SetValue(MaxLengthProperty, value);
  86. }
  87. }
  88. // Using a DependencyProperty as the backing store for MaxLength. This enables animation, styling, binding, etc...
  89. public static readonly DependencyProperty MaxLengthProperty =
  90. DependencyProperty.Register("MaxLength", typeof(int), typeof(EditableTextBlock), new UIPropertyMetadata(0));
  91. private void TextBoxLostFocus(object sender, RoutedEventArgs e)
  92. {
  93. IsInEditMode = false;
  94. }
  95. /// <summary>
  96. /// release the edit mode when user presses enter.
  97. /// </summary>
  98. /// <param name="sender">The sender.</param>
  99. /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
  100. private void TextBoxKeyUp(object sender, KeyEventArgs e)
  101. {
  102. if (e.Key == Key.Enter)
  103. {
  104. IsInEditMode = false;
  105. }
  106. }
  107. /// <summary>
  108. /// 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.
  109. /// </summary>
  110. /// <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>
  111. protected override void OnMouseDown(MouseButtonEventArgs e)
  112. {
  113. if (e.MiddleButton == MouseButtonState.Pressed)
  114. {
  115. IsInEditMode = true;
  116. }
  117. else if (e.ClickCount == 2)
  118. {
  119. IsInEditMode = true;
  120. }
  121. }
  122. }
  123. }