SearchContent.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using PDF_Office.CustomControl;
  2. using PDF_Office.Model.BOTA;
  3. using PDF_Office.ViewModels.BOTA;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. namespace PDF_Office.Views.BOTA
  22. {
  23. /// <summary>
  24. /// SearchContent.xaml 的交互逻辑
  25. /// </summary>
  26. public partial class SearchContent : UserControl
  27. {
  28. public SearchContent()
  29. {
  30. InitializeComponent();
  31. }
  32. List<string> HistorySearchText = new List<string>();
  33. const int MaxHistoryCount = 10;
  34. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  35. {
  36. if (e.Key == Key.Enter)
  37. {
  38. TextBox text = e.Source as TextBox;
  39. if (text == null)
  40. {
  41. return;
  42. }
  43. if (string.IsNullOrEmpty(text.Text))
  44. {
  45. return;
  46. }
  47. (DataContext as SearchContentViewModel).SearchText(text.Text);
  48. HistorySearchText.Remove(text.Text);
  49. if (HistorySearchText.Count >= MaxHistoryCount)
  50. {
  51. HistorySearchText.RemoveAt(HistorySearchText.Count - 1);
  52. }
  53. HistorySearchText.Insert(0, text.Text);
  54. }
  55. }
  56. /// <summary>
  57. /// 控制收起与展开
  58. /// </summary>
  59. private void BtnExptend_Click(object sender, RoutedEventArgs e)
  60. {
  61. var btn = sender as PathButton;
  62. if (btn == null) return;
  63. btn.IsChecked = !btn.IsChecked;
  64. var item = (sender as FrameworkElement).DataContext as CollectionViewGroup;
  65. if (item != null)
  66. {
  67. foreach (object item2 in item.Items)
  68. {
  69. SearchItem searchItem = item2 as SearchItem;
  70. if (searchItem.TextProperty.ItemVisibility == Visibility.Visible)
  71. {
  72. searchItem.TextProperty.ItemVisibility = Visibility.Collapsed;
  73. }
  74. else
  75. {
  76. searchItem.TextProperty.ItemVisibility = Visibility.Visible;
  77. }
  78. }
  79. }
  80. }
  81. private void Create_Click(object sender, RoutedEventArgs e)
  82. {
  83. MenuItem menuItem = sender as MenuItem;
  84. if (menuItem == null)
  85. {
  86. return;
  87. }
  88. foreach (object item in SearchResultList.SelectedItems)
  89. {
  90. (DataContext as SearchContentViewModel).CreateAnnotate(item, menuItem.Tag.ToString());
  91. }
  92. }
  93. private void TextBoxEx_Initialized(object sender, EventArgs e)
  94. {
  95. var btn = sender as TextBoxEx;
  96. if (btn != null)
  97. {
  98. btn.ContextMenu = null;
  99. }
  100. }
  101. private void PathButton_Click(object sender, RoutedEventArgs e)
  102. {
  103. ContextSearchText.PlacementTarget = SearchText;
  104. ContextSearchText.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
  105. ContextSearchText.IsOpen = true;
  106. UpDataItem();
  107. }
  108. private void UpDataItem()
  109. {
  110. foreach (var item in ContextSearchText.Items)
  111. {
  112. if (item is MenuItem)
  113. {
  114. (item as MenuItem).Click -= SearchContent_Click;
  115. }
  116. }
  117. ContextSearchText.Items.Clear();
  118. ContextSearchText.Items.Add(IgnoreItem);
  119. if (HistorySearchText.Count > 0)
  120. {
  121. ContextSearchText.Items.Add(IgnoreSeparator);
  122. }
  123. foreach (var item in HistorySearchText)
  124. {
  125. MenuItem Searchitem = new MenuItem();
  126. Searchitem.Height = 32;
  127. Searchitem.Header = item;
  128. Searchitem.Padding = new Thickness(0, 7, 0, 7);
  129. Searchitem.Click += SearchContent_Click;
  130. ContextSearchText.Items.Add(Searchitem);
  131. }
  132. if (HistorySearchText.Count > 0)
  133. {
  134. ContextSearchText.Items.Add(ClearSeparator);
  135. ContextSearchText.Items.Add(ClearItem);
  136. }
  137. }
  138. private void SearchContent_Click(object sender, RoutedEventArgs e)
  139. {
  140. MenuItem item = sender as MenuItem;
  141. if (item != null)
  142. {
  143. SearchText.Text = item.Header.ToString();
  144. (DataContext as SearchContentViewModel).SearchText(SearchText.Text);
  145. }
  146. }
  147. private void ClearItem_Click(object sender, RoutedEventArgs e)
  148. {
  149. HistorySearchText.Clear();
  150. UpDataItem();
  151. }
  152. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  153. {
  154. SearchText.Focus();
  155. }
  156. private void UserControl_KeyDown(object sender, KeyEventArgs e)
  157. {
  158. if (e.Key == Key.Escape)
  159. {
  160. SearchResultList.SelectedIndex = -1;
  161. e.Handled = true;
  162. }
  163. }
  164. }
  165. }