SearchContent.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using PDF_Master.CustomControl;
  2. using PDF_Master.EventAggregators;
  3. using PDF_Master.Model.BOTA;
  4. using PDF_Master.ViewModels.BOTA;
  5. using Prism.Events;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Runtime.CompilerServices;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace PDF_Master.Views.BOTA
  24. {
  25. /// <summary>
  26. /// SearchContent.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class SearchContent : UserControl
  29. {
  30. bool Updata = false;
  31. private string unicode = "";
  32. public SearchContent()
  33. {
  34. InitializeComponent();
  35. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  36. }
  37. public SearchContent(IEventAggregator eventAggregator) : this()
  38. {
  39. eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  40. }
  41. private void CleanSelectAll(CleanSelectAllArgs obj)
  42. {
  43. SearchResultList.SelectedIndex = -1;
  44. (DataContext as SearchContentViewModel).CleaSelect();
  45. if (Updata)
  46. {
  47. if (string.IsNullOrEmpty(SearchText.Text))
  48. {
  49. return;
  50. }
  51. (DataContext as SearchContentViewModel).SearchText(SearchText.Text);
  52. HistorySearchText.Remove(SearchText.Text);
  53. if (HistorySearchText.Count >= MaxHistoryCount)
  54. {
  55. HistorySearchText.RemoveAt(HistorySearchText.Count - 1);
  56. }
  57. HistorySearchText.Insert(0, SearchText.Text);
  58. }
  59. }
  60. List<string> HistorySearchText = new List<string>();
  61. const int MaxHistoryCount = 10;
  62. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  63. {
  64. if (e.Key == Key.Enter)
  65. {
  66. TextBox text = e.Source as TextBox;
  67. if (text == null)
  68. {
  69. return;
  70. }
  71. if (string.IsNullOrEmpty(text.Text))
  72. {
  73. return;
  74. }
  75. (DataContext as SearchContentViewModel).SearchText(text.Text);
  76. HistorySearchText.Remove(text.Text);
  77. if (HistorySearchText.Count >= MaxHistoryCount)
  78. {
  79. HistorySearchText.RemoveAt(HistorySearchText.Count - 1);
  80. }
  81. HistorySearchText.Insert(0, text.Text);
  82. }
  83. }
  84. /// <summary>
  85. /// 控制收起与展开
  86. /// </summary>
  87. private void BtnExptend_Click(object sender, RoutedEventArgs e)
  88. {
  89. var btn = sender as PathButton;
  90. if (btn == null) return;
  91. btn.IsChecked = !btn.IsChecked;
  92. var item = (sender as FrameworkElement).DataContext as CollectionViewGroup;
  93. if (item != null)
  94. {
  95. foreach (object item2 in item.Items)
  96. {
  97. SearchItem searchItem = item2 as SearchItem;
  98. if (searchItem.TextProperty.ItemVisibility == Visibility.Visible)
  99. {
  100. searchItem.TextProperty.ItemVisibility = Visibility.Collapsed;
  101. }
  102. else
  103. {
  104. searchItem.TextProperty.ItemVisibility = Visibility.Visible;
  105. }
  106. }
  107. }
  108. }
  109. private void Create_Click(object sender, RoutedEventArgs e)
  110. {
  111. MenuItem menuItem = sender as MenuItem;
  112. if (menuItem == null)
  113. {
  114. return;
  115. }
  116. foreach (object item in SearchResultList.SelectedItems)
  117. {
  118. (DataContext as SearchContentViewModel).CreateAnnotate(item, menuItem.Tag.ToString());
  119. }
  120. }
  121. private void TextBoxEx_Initialized(object sender, EventArgs e)
  122. {
  123. var btn = sender as TextBoxEx;
  124. if (btn != null)
  125. {
  126. btn.ContextMenu = null;
  127. }
  128. }
  129. private void PathButton_Click(object sender, RoutedEventArgs e)
  130. {
  131. ContextSearchText.PlacementTarget = SearchText;
  132. ContextSearchText.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
  133. ContextSearchText.IsOpen = true;
  134. UpDataItem();
  135. }
  136. private void UpDataItem()
  137. {
  138. foreach (var item in ContextSearchText.Items)
  139. {
  140. if (item is MenuItem)
  141. {
  142. (item as MenuItem).Click -= SearchContent_Click;
  143. }
  144. }
  145. ContextSearchText.Items.Clear();
  146. ContextSearchText.Items.Add(IgnoreItem);
  147. if (HistorySearchText.Count > 0)
  148. {
  149. ContextSearchText.Items.Add(IgnoreSeparator);
  150. }
  151. foreach (var item in HistorySearchText)
  152. {
  153. MenuItem Searchitem = new MenuItem();
  154. Searchitem.Height = 32;
  155. Searchitem.Header = item;
  156. Searchitem.Padding = new Thickness(0, 7, 0, 7);
  157. Searchitem.Click += SearchContent_Click;
  158. ContextSearchText.Items.Add(Searchitem);
  159. }
  160. if (HistorySearchText.Count > 0)
  161. {
  162. ContextSearchText.Items.Add(ClearSeparator);
  163. ContextSearchText.Items.Add(ClearItem);
  164. }
  165. }
  166. private void SearchContent_Click(object sender, RoutedEventArgs e)
  167. {
  168. MenuItem item = sender as MenuItem;
  169. if (item != null)
  170. {
  171. SearchText.Text = item.Header.ToString();
  172. (DataContext as SearchContentViewModel).SearchText(SearchText.Text);
  173. }
  174. }
  175. private void ClearItem_Click(object sender, RoutedEventArgs e)
  176. {
  177. HistorySearchText.Clear();
  178. UpDataItem();
  179. }
  180. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  181. {
  182. SearchText.Focus();
  183. }
  184. private void UserControl_KeyDown(object sender, KeyEventArgs e)
  185. {
  186. if (e.Key == Key.Escape)
  187. {
  188. SearchResultList.SelectedIndex = -1;
  189. e.Handled = true;
  190. }
  191. }
  192. private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
  193. {
  194. SearchContent searchContent = sender as SearchContent;
  195. if (searchContent != null && searchContent.ActualWidth > 209 && searchContent.ActualWidth < 216)
  196. {
  197. SearchText.Focus();
  198. }
  199. }
  200. private void SearchResultList_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  201. {
  202. if (e.LeftButton == MouseButtonState.Pressed)
  203. {
  204. var pos = e.GetPosition(SearchResultList);
  205. var result = VisualTreeHelper.HitTest(SearchResultList, pos);
  206. if (result != null)
  207. {
  208. //获取当前鼠标指针下的容器
  209. var listBoxItem = Helper.CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  210. if (listBoxItem == null)
  211. {
  212. if (SearchResultList.SelectedItem != null)
  213. {
  214. SearchResultList.SelectedIndex = -1;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. private void SearchText_LostFocus(object sender, RoutedEventArgs e)
  221. {
  222. Updata = false;
  223. }
  224. private void SearchText_GotFocus(object sender, RoutedEventArgs e)
  225. {
  226. Updata = true;
  227. }
  228. }
  229. }