AutoCompleteTextBox.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Input;
  4. using HandyControl.Data;
  5. using HandyControl.Tools.Helper;
  6. namespace HandyControl.Controls;
  7. [TemplatePart(Name = SearchTextBox, Type = typeof(System.Windows.Controls.TextBox))]
  8. public class AutoCompleteTextBox : ComboBox
  9. {
  10. private const string SearchTextBox = "PART_SearchTextBox";
  11. private bool ignoreTextChanging;
  12. private System.Windows.Controls.TextBox _searchTextBox;
  13. private object _selectedItem;
  14. static AutoCompleteTextBox()
  15. {
  16. TextProperty.OverrideMetadata(typeof(AutoCompleteTextBox), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
  17. }
  18. public override void OnApplyTemplate()
  19. {
  20. if (_searchTextBox != null)
  21. {
  22. _searchTextBox.GotFocus -= SearchTextBoxGotFocus;
  23. _searchTextBox.KeyDown -= SearchTextBoxKeyDown;
  24. _searchTextBox.TextChanged -= SearchTextBoxTextChanged;
  25. }
  26. base.OnApplyTemplate();
  27. _searchTextBox = GetTemplateChild(SearchTextBox) as System.Windows.Controls.TextBox;
  28. if (_searchTextBox != null)
  29. {
  30. _searchTextBox.GotFocus += SearchTextBoxGotFocus;
  31. _searchTextBox.PreviewKeyDown += SearchTextBoxKeyDown;
  32. _searchTextBox.TextChanged += SearchTextBoxTextChanged;
  33. }
  34. UpdateTextBoxBySelectedItem(_selectedItem);
  35. }
  36. protected override void OnSelectionChanged(SelectionChangedEventArgs e)
  37. {
  38. base.OnSelectionChanged(e);
  39. if (e.AddedItems.Count > 0)
  40. {
  41. _selectedItem = e.AddedItems[0];
  42. UpdateTextBoxBySelectedItem(_selectedItem);
  43. }
  44. }
  45. protected override bool IsItemItsOwnContainerOverride(object item) => item is AutoCompleteTextBoxItem;
  46. protected override DependencyObject GetContainerForItemOverride() => new AutoCompleteTextBoxItem();
  47. private void SearchTextBoxTextChanged(object sender, TextChangedEventArgs e)
  48. {
  49. _selectedItem = null;
  50. SelectedIndex = -1;
  51. if (ignoreTextChanging)
  52. {
  53. ignoreTextChanging = false;
  54. return;
  55. }
  56. Text = _searchTextBox.Text;
  57. if (string.IsNullOrEmpty(Text))
  58. {
  59. SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.FalseBox);
  60. _searchTextBox.Focus();
  61. }
  62. else if (_searchTextBox.IsFocused)
  63. {
  64. SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.TrueBox);
  65. }
  66. }
  67. private void SearchTextBoxKeyDown(object sender, KeyEventArgs e)
  68. {
  69. if (e.Key == Key.Up)
  70. {
  71. var index = SelectedIndex - 1;
  72. if (index < 0)
  73. {
  74. index = Items.Count - 1;
  75. }
  76. UpdateTextBoxBySelectedIndex(index);
  77. }
  78. else if (e.Key == Key.Down)
  79. {
  80. var index = SelectedIndex + 1;
  81. if (index >= Items.Count)
  82. {
  83. index = 0;
  84. }
  85. UpdateTextBoxBySelectedIndex(index);
  86. }
  87. else if (e.Key == Key.Enter)
  88. {
  89. SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.FalseBox);
  90. e.Handled = true;
  91. }
  92. }
  93. private void UpdateTextBoxBySelectedIndex(int selectedIndex)
  94. {
  95. if (_searchTextBox == null)
  96. {
  97. return;
  98. }
  99. ignoreTextChanging = true;
  100. if (ItemContainerGenerator.ContainerFromIndex(selectedIndex) is AutoCompleteTextBoxItem boxItem)
  101. {
  102. _searchTextBox.Text = BindingHelper.GetString(boxItem.Content, DisplayMemberPath);
  103. _searchTextBox.CaretIndex = _searchTextBox.Text.Length;
  104. SelectedIndex = selectedIndex;
  105. }
  106. }
  107. private void UpdateTextBoxBySelectedItem(object selectedItem)
  108. {
  109. if (_searchTextBox == null)
  110. {
  111. return;
  112. }
  113. ignoreTextChanging = true;
  114. _searchTextBox.Text = BindingHelper.GetString(selectedItem, DisplayMemberPath);
  115. _searchTextBox.CaretIndex = _searchTextBox.Text.Length;
  116. ignoreTextChanging = true;
  117. Text = _searchTextBox.Text;
  118. ignoreTextChanging = false;
  119. }
  120. private void SearchTextBoxGotFocus(object sender, RoutedEventArgs e)
  121. {
  122. if (!string.IsNullOrEmpty(Text))
  123. {
  124. SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.TrueBox);
  125. }
  126. }
  127. }