CPDFBookmarkResultUI.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Controls.Primitives;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace compdfkit_tools.PDFControlUI
  20. {
  21. public partial class CPDFBookmarkResultUI : UserControl
  22. {
  23. /// <summary>
  24. /// 书签列表点击选中更改事件
  25. /// </summary>
  26. public event EventHandler<int> SelectionChanged;
  27. /// <summary>
  28. /// 搜索结果列表点击选中事件
  29. /// </summary>
  30. public event EventHandler<BookmarkChangeData> BookmarkChanged;
  31. /// <summary>
  32. /// 书签删除点击事件
  33. /// </summary>
  34. public event EventHandler<BookmarkChangeData> BookmarkDelete;
  35. /// <summary>
  36. /// 点击书签事件
  37. /// </summary>
  38. public event EventHandler<int> BookmarkClicked;
  39. /// <summary>
  40. /// 绑定书签结果集合
  41. /// </summary>
  42. private ObservableCollection<BookmarkBindData> bookmarkResults;
  43. public CPDFBookmarkResultUI()
  44. {
  45. InitializeComponent();
  46. bookmarkResults = new ObservableCollection<BookmarkBindData>();
  47. ICollectionView groupView = CollectionViewSource.GetDefaultView(bookmarkResults);
  48. groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(BookmarkBindData.ShowPageIndex)));
  49. }
  50. /// <summary>
  51. /// 鼠标移入事件 用以控制展示右侧编辑删除面板
  52. /// </summary>
  53. private void Grid_MouseEnter(object sender, MouseEventArgs e)
  54. {
  55. Grid sourceGrid=sender as Grid;
  56. if(sourceGrid!=null && sourceGrid.Children.Count==2)
  57. {
  58. Border sourceBorder = sourceGrid.Children[1] as Border;
  59. if(sourceBorder!=null)
  60. {
  61. sourceBorder.Visibility = Visibility.Visible;
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// 鼠标移出事件 用以控制隐藏右侧编辑 删除面板
  67. /// </summary>
  68. private void Grid_MouseLeave(object sender, MouseEventArgs e)
  69. {
  70. Grid sourceGrid = sender as Grid;
  71. if (sourceGrid != null && sourceGrid.Children.Count == 2)
  72. {
  73. Border sourceBorder = sourceGrid.Children[1] as Border;
  74. if (sourceBorder != null)
  75. {
  76. sourceBorder.Visibility = Visibility.Collapsed;
  77. }
  78. TextBox sourceTextBox = sourceGrid.Children[0] as TextBox;
  79. if (sourceTextBox != null)
  80. {
  81. sourceTextBox.IsReadOnly = true;
  82. sourceTextBox.BorderThickness = new Thickness(0);
  83. sourceTextBox.IsHitTestVisible = false;
  84. BookmarkBindData bindData=sourceGrid.Tag as BookmarkBindData;
  85. if (bindData!=null && IsBookmarkChanged(bindData,sourceTextBox.Text))
  86. {
  87. BookmarkChanged?.Invoke(this, new BookmarkChangeData()
  88. {
  89. PageIndex=bindData.BindProperty.PageIndex,
  90. BookmarkTitle=bindData.BindProperty.BookmarkTitle,
  91. NewTitle=sourceTextBox.Text
  92. });
  93. }
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// 编辑按钮点击事件 启用书签文本编辑
  99. /// </summary>
  100. private void EditBorder_Click(object sender, RoutedEventArgs e)
  101. {
  102. Border sourceBtn =sender as Border;
  103. if(sourceBtn != null)
  104. {
  105. DependencyObject findElement = null;
  106. if (FindParent<Grid>(sourceBtn, out findElement) && findElement != null)
  107. {
  108. Grid sourceGrid = findElement as Grid;
  109. if (sourceGrid != null && sourceGrid.Children.Count == 2)
  110. {
  111. TextBox sourceTextBox = sourceGrid.Children[0] as TextBox;
  112. if (sourceTextBox != null)
  113. {
  114. sourceTextBox.IsReadOnly=false;
  115. sourceTextBox.IsHitTestVisible = true;
  116. sourceTextBox.BorderThickness=new Thickness(1);
  117. }
  118. }
  119. }
  120. }
  121. e.Handled = true;
  122. }
  123. /// <summary>
  124. /// 删除按钮点击事件 触发删除事件通知
  125. /// </summary>
  126. private void DelBorder_Click(object sender, RoutedEventArgs e)
  127. {
  128. Border sourceBtn = sender as Border;
  129. if (sourceBtn != null)
  130. {
  131. DependencyObject findElement = null;
  132. if (FindParent<Grid>(sourceBtn, out findElement) && findElement != null)
  133. {
  134. Grid sourceGrid = findElement as Grid;
  135. BookmarkBindData bindData = sourceGrid.Tag as BookmarkBindData;
  136. if (bindData != null)
  137. {
  138. BookmarkDelete?.Invoke(this, new BookmarkChangeData()
  139. {
  140. PageIndex = bindData.BindProperty.PageIndex,
  141. BookmarkTitle = bindData.BindProperty.BookmarkTitle
  142. });
  143. bookmarkResults?.Remove(bindData);
  144. }
  145. }
  146. }
  147. e.Handled = true;
  148. }
  149. /// <summary>
  150. /// 递归查询指定类型的父节点元素
  151. /// </summary>
  152. /// <typeparam name="T">父元素节点类型</typeparam>
  153. /// <param name="checkElement">要查找的元素</param>
  154. /// <param name="parent">查找到的父节点元素</param>
  155. /// <returns>查找到对应类型父节点元素则为true</returns>
  156. private bool FindParent<T>(DependencyObject checkElement,out DependencyObject parent)
  157. {
  158. parent = null;
  159. try
  160. {
  161. if (checkElement != null)
  162. {
  163. DependencyObject parentElement= VisualTreeHelper.GetParent(checkElement);
  164. if(parentElement!=null && parentElement is T)
  165. {
  166. parent =parentElement;
  167. return true;
  168. }
  169. return FindParent<T>(parentElement,out parent);
  170. }
  171. }
  172. catch (Exception ex)
  173. {
  174. }
  175. return false;
  176. }
  177. /// <summary>
  178. /// 判断书签文本是否更改
  179. /// </summary>
  180. private bool IsBookmarkChanged(BookmarkBindData bindData,string newTitle)
  181. {
  182. if(bindData!=null &&bindData.BindProperty!=null)
  183. {
  184. return bindData.BindProperty.BookmarkTitle != newTitle;
  185. }
  186. return false;
  187. }
  188. /// <summary>
  189. /// 将书签列表绑定到UI控件
  190. /// </summary>
  191. public void SetBookmarkResult(List<BindBookmarkResult> results)
  192. {
  193. bookmarkResults?.Clear();
  194. if (results == null || results.Count == 0)
  195. {
  196. ResultListControl.ItemsSource = null;
  197. return;
  198. }
  199. foreach (BindBookmarkResult item in results)
  200. {
  201. bookmarkResults.Add(new BookmarkBindData()
  202. {
  203. BindProperty = item
  204. });
  205. }
  206. ResultListControl.ItemsSource = bookmarkResults;
  207. }
  208. /// <summary>
  209. /// 获取选中书签结果
  210. /// </summary>
  211. /// <returns>搜索结果</returns>
  212. public BindBookmarkResult GetSelectItem()
  213. {
  214. BookmarkBindData bindData = ResultListControl.SelectedItem as BookmarkBindData;
  215. if (bindData != null)
  216. {
  217. return bindData.BindProperty;
  218. }
  219. return null;
  220. }
  221. /// <summary>
  222. /// 获取指定索引书签对象
  223. /// </summary>
  224. /// <param name="index">指定索引</param>
  225. /// <returns></returns>
  226. public BindBookmarkResult GetItem(int index)
  227. {
  228. if (index < 0)
  229. {
  230. return null;
  231. }
  232. if (ResultListControl.HasItems && ResultListControl.Items.Count > index)
  233. {
  234. BookmarkBindData bindData = ResultListControl.Items[index] as BookmarkBindData;
  235. if (bindData != null)
  236. {
  237. return bindData.BindProperty;
  238. }
  239. }
  240. return null;
  241. }
  242. /// <summary>
  243. /// 清除选中结果
  244. /// </summary>
  245. public void ClearSelection()
  246. {
  247. int oldSelectionIndex = ResultListControl.SelectedIndex;
  248. ResultListControl.UnselectAll();
  249. if (oldSelectionIndex != ResultListControl.SelectedIndex)
  250. {
  251. SelectionChanged?.Invoke(this,ResultListControl.SelectedIndex);
  252. }
  253. }
  254. /// <summary>
  255. /// 设置选中结果
  256. /// </summary>
  257. /// <param name="selectIndex">选中索引</param>
  258. public void SelectItem(int selectIndex)
  259. {
  260. if (ResultListControl.SelectedIndex != selectIndex)
  261. {
  262. ResultListControl.SelectedIndex = selectIndex;
  263. }
  264. }
  265. /// <summary>
  266. /// 书签列表选中改变事件
  267. /// </summary>
  268. private void ResultListControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  269. {
  270. SelectionChanged?.Invoke(this, ResultListControl.SelectedIndex);
  271. }
  272. /// <summary>
  273. /// 书签列表点击书签事件
  274. /// </summary>
  275. private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  276. {
  277. Grid sourceGrid = sender as Grid;
  278. if (sourceGrid != null)
  279. {
  280. BookmarkBindData bindData = sourceGrid.Tag as BookmarkBindData;
  281. if (bindData != null)
  282. {
  283. BookmarkClicked?.Invoke(this, bindData.BindProperty.PageIndex);
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// 点击空白清除选中项
  289. /// </summary>
  290. private void ResultListControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  291. {
  292. ResultListControl?.UnselectAll();
  293. }
  294. }
  295. public class BookmarkChangeData
  296. {
  297. /// <summary>
  298. /// 页面索引
  299. /// </summary>
  300. public int PageIndex { get; set; }
  301. /// <summary>
  302. /// 原书签标题
  303. /// </summary>
  304. public string BookmarkTitle { get; set; }
  305. /// <summary>
  306. /// 修改后的标题
  307. /// </summary>
  308. public string NewTitle { get;set; }
  309. }
  310. public class BindBookmarkResult
  311. {
  312. /// <summary>
  313. /// 页面索引
  314. /// </summary>
  315. public int PageIndex { get; set; }
  316. /// <summary>
  317. /// 书签标题
  318. /// </summary>
  319. public string BookmarkTitle { get; set; }
  320. }
  321. internal class BookmarkBindData
  322. {
  323. public int ShowPageIndex { get { return BindProperty.PageIndex + 1; } set { BindProperty.PageIndex = value; } }
  324. public BindBookmarkResult BindProperty { get; set; }
  325. public BookmarkBindData()
  326. {
  327. BindProperty = new BindBookmarkResult();
  328. }
  329. }
  330. }