ComboTextBlockAdorner.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 ComboTextBlockAdorner: Adorner
  11. {
  12. public ComboBoxExt TextBox
  13. {
  14. get { return this._comboBox; }
  15. }
  16. private readonly VisualCollection _collection;
  17. private readonly ComboBoxExt _comboBox;
  18. private readonly ComboTextBlock _textBlock;
  19. private TextBox _textboxInEditableComboBox;
  20. public ComboTextBlockAdorner(ComboTextBlock adornedElement)
  21. : base(adornedElement)
  22. {
  23. _collection = new VisualCollection(this);
  24. _comboBox = new ComboBoxExt();
  25. _comboBox.DisplayMemberPath = "DisplayName";
  26. _comboBox.IsEditable = adornedElement.IsEditable;
  27. _textBlock = adornedElement;
  28. Binding binding = new Binding("ItemsSource") { Source = adornedElement };
  29. binding.Mode = BindingMode.TwoWay;
  30. _comboBox.SetBinding(ComboBoxExt.ItemsSourceProperty, binding);
  31. binding = new Binding("SelectedItem") { Source = adornedElement };
  32. binding.Mode = BindingMode.TwoWay;
  33. _comboBox.SetBinding(ComboBoxExt.SelectedItemProperty, binding);
  34. binding = new Binding("Text") { Source = adornedElement };
  35. binding.Mode = BindingMode.TwoWay;
  36. _comboBox.SetBinding(ComboBoxExt.TextProperty, binding);
  37. _comboBox.Text = _textBlock.Text;
  38. binding = new Binding("TextSaved") { Source = adornedElement };
  39. binding.Mode = BindingMode.TwoWay;
  40. _comboBox.SetBinding(ComboBoxExt.ComboBoxSavedProperty, binding);
  41. //_textBox.KeyUp += _textBox_KeyUp;
  42. _comboBox.Loaded += _comboBox_Loaded;
  43. _comboBox.Unloaded += _comboBox_Unloaded;
  44. _collection.Add(_comboBox);
  45. }
  46. private void _comboBox_Unloaded(object sender, RoutedEventArgs e)
  47. {
  48. if (_comboBox.IsEditable)
  49. {
  50. _textboxInEditableComboBox = (_comboBox.Template.FindName("PART_EditableTextBox", _comboBox) as TextBox);
  51. if (_textboxInEditableComboBox != null)
  52. {
  53. _textboxInEditableComboBox.LostFocus -= _textBlock.TextBoxLostFocus;
  54. }
  55. }
  56. else
  57. {
  58. //_comboBox.SelectionChanged -= _textBlock.TextBoxLostFocus;
  59. _comboBox.DropDownClosed -= _textBlock.DropDownClosedLostFocus;
  60. }
  61. }
  62. private void _comboBox_Loaded(object sender, RoutedEventArgs e)
  63. {
  64. if (_comboBox.IsEditable)
  65. {
  66. _textboxInEditableComboBox = (_comboBox.Template.FindName("PART_EditableTextBox", _comboBox) as TextBox);
  67. if (_textboxInEditableComboBox != null)
  68. {
  69. _textboxInEditableComboBox.LostFocus += _textBlock.TextBoxLostFocus;
  70. _textboxInEditableComboBox.Focus();
  71. }
  72. }
  73. else
  74. {
  75. //_comboBox.SelectionChanged += _textBlock.TextBoxLostFocus;
  76. _comboBox.IsDropDownOpen = true;
  77. _comboBox.DropDownClosed += _textBlock.DropDownClosedLostFocus;
  78. }
  79. }
  80. public event RoutedEventHandler TextBoxLostFocus
  81. {
  82. add
  83. {
  84. if (_textboxInEditableComboBox != null)
  85. _textboxInEditableComboBox.LostFocus += value;
  86. }
  87. remove
  88. {
  89. //var textBox = (_comboBox.Template.FindName("PART_EditableTextBox", _comboBox) as TextBox);
  90. if (_textboxInEditableComboBox != null)
  91. _textboxInEditableComboBox.LostFocus -= value;
  92. }
  93. }
  94. //public event KeyEventHandler TextBoxKeyUp
  95. //{
  96. // add
  97. // {
  98. // _textBox.KeyUp += value;
  99. // }
  100. // remove
  101. // {
  102. // _textBox.KeyUp -= value;
  103. // }
  104. //}
  105. protected override Visual GetVisualChild(int index)
  106. {
  107. return _collection[index];
  108. }
  109. protected override int VisualChildrenCount
  110. {
  111. get
  112. {
  113. return _collection.Count;
  114. }
  115. }
  116. protected override Size ArrangeOverride(Size finalSize)
  117. {
  118. _comboBox.Arrange(new Rect(0, 0, _textBlock.ActualWidth, _textBlock.ActualHeight));
  119. //if (_textbox == null)
  120. // _comboBox.Focus();
  121. //_comboBox.ScrollToEnd();
  122. return finalSize;
  123. }
  124. private void _textBox_KeyUp(object sender, KeyEventArgs e)
  125. {
  126. //if (e.Key == Key.Enter)
  127. //{
  128. // _textBox.Text = _textBox.Text.Replace("\r\n", string.Empty);
  129. // BindingExpression expression = _textBox.GetBindingExpression(TextBoxEx.TextProperty);
  130. // if (null != expression)
  131. // expression.UpdateSource();
  132. // expression = _textBox.GetBindingExpression(TextBoxEx.TextSavedProperty);
  133. // if (null != expression)
  134. // expression.UpdateSource();
  135. //}
  136. }
  137. }
  138. }