EditTextBlockAdorner.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.Media;
  7. using System.Windows.Input;
  8. namespace OpenSEMI.Ctrlib.Controls
  9. {
  10. public class EditTextBlockAdorner: Adorner
  11. {
  12. public TextBoxEx TextBox
  13. {
  14. get { return this._textBox; }
  15. }
  16. private readonly VisualCollection _collection;
  17. private readonly TextBoxEx _textBox;
  18. private readonly TextBlock _textBlock;
  19. public EditTextBlockAdorner(EditTextBlock adornedElement)
  20. : base(adornedElement)
  21. {
  22. _collection = new VisualCollection(this);
  23. _textBox = new TextBoxEx();
  24. _textBlock = adornedElement;
  25. Binding binding = new Binding("Text") { Source = adornedElement };
  26. binding.Mode = BindingMode.TwoWay;
  27. _textBox.SetBinding(TextBoxEx.TextProperty, binding);
  28. _textBox.EditBoxMode = adornedElement.EditBoxMode;
  29. _textBox.AllowEmpty = adornedElement.AllowEmpty;
  30. _textBox.AllowBackgroundChange = adornedElement.AllowBackgroundChange;
  31. _textBox.MaxValue = adornedElement.MaxValue;
  32. _textBox.MinValue = adornedElement.MinValue;
  33. _textBox.Accuracy = adornedElement.Accuracy;
  34. _textBox.IsScrollToEnd = adornedElement.IsScrollToEnd;
  35. binding = new Binding("TextSaved") { Source = adornedElement };
  36. binding.Mode = BindingMode.TwoWay;
  37. _textBox.SetBinding(TextBoxEx.TextSavedProperty, binding);
  38. _textBox.KeyUp += _textBox_KeyUp;
  39. _collection.Add(_textBox);
  40. if (!adornedElement.IsEnabled)
  41. _textBlock.Background = Brushes.Gray;
  42. }
  43. public event RoutedEventHandler TextBoxLostFocus
  44. {
  45. add
  46. {
  47. _textBox.LostFocus += value;
  48. }
  49. remove
  50. {
  51. _textBox.LostFocus -= value;
  52. }
  53. }
  54. public event KeyEventHandler TextBoxKeyUp
  55. {
  56. add
  57. {
  58. _textBox.KeyUp += value;
  59. }
  60. remove
  61. {
  62. _textBox.KeyUp -= value;
  63. }
  64. }
  65. protected override Visual GetVisualChild(int index)
  66. {
  67. return _collection[index];
  68. }
  69. protected override int VisualChildrenCount
  70. {
  71. get
  72. {
  73. return _collection.Count;
  74. }
  75. }
  76. protected override Size ArrangeOverride(Size finalSize)
  77. {
  78. _textBox.Arrange(new Rect(0, 0, _textBlock.ActualWidth, _textBlock.ActualHeight));
  79. _textBox.Focus();
  80. _textBox.ScrollToEnd();
  81. return finalSize;
  82. }
  83. private void _textBox_KeyUp(object sender, KeyEventArgs e)
  84. {
  85. if (e.Key == Key.Enter)
  86. {
  87. _textBox.Text = _textBox.Text.Replace("\r\n", string.Empty);
  88. BindingExpression expression = _textBox.GetBindingExpression(TextBoxEx.TextProperty);
  89. if (null != expression)
  90. expression.UpdateSource();
  91. expression = _textBox.GetBindingExpression(TextBoxEx.TextSavedProperty);
  92. if (null != expression)
  93. expression.UpdateSource();
  94. }
  95. }
  96. }
  97. }