CPDFBookmarkResultUI.xaml.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. namespace ComPDFKit.Controls.PDFControlUI
  11. {
  12. public partial class CPDFBookmarkResultUI : UserControl
  13. {
  14. public event EventHandler<int> SelectionChanged;
  15. public event EventHandler<BookmarkChangeData> BookmarkDelete;
  16. public event EventHandler<BookmarkChangeData> BookmarkEdit;
  17. public event EventHandler<int> BookmarkClicked;
  18. private ObservableCollection<BookmarkBindData> bookmarkResults;
  19. public CPDFBookmarkResultUI()
  20. {
  21. InitializeComponent();
  22. bookmarkResults = new ObservableCollection<BookmarkBindData>();
  23. ICollectionView groupView = CollectionViewSource.GetDefaultView(bookmarkResults);
  24. groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(BookmarkBindData.ShowPageIndex)));
  25. }
  26. private void Grid_MouseEnter(object sender, MouseEventArgs e)
  27. {
  28. Grid sourceGrid=sender as Grid;
  29. if(sourceGrid!=null && sourceGrid.Children.Count==2)
  30. {
  31. Border sourceBorder = sourceGrid.Children[1] as Border;
  32. if(sourceBorder!=null)
  33. {
  34. sourceBorder.Visibility = Visibility.Visible;
  35. }
  36. }
  37. }
  38. private void Grid_MouseLeave(object sender, MouseEventArgs e)
  39. {
  40. Grid sourceGrid = sender as Grid;
  41. if (sourceGrid != null && sourceGrid.Children.Count == 2)
  42. {
  43. Border sourceBorder = sourceGrid.Children[1] as Border;
  44. if (sourceBorder != null)
  45. {
  46. sourceBorder.Visibility = Visibility.Collapsed;
  47. }
  48. }
  49. }
  50. private void EditBorder_Click(object sender, RoutedEventArgs e)
  51. {
  52. Border sourceBtn =sender as Border;
  53. if(sourceBtn != null)
  54. {
  55. DependencyObject findElement = null;
  56. if (FindParent<Grid>(sourceBtn, out findElement) && findElement != null)
  57. {
  58. Grid sourceGrid = findElement as Grid;
  59. BookmarkBindData bindData = sourceGrid.Tag as BookmarkBindData;
  60. if (bindData != null)
  61. {
  62. BookmarkEdit?.Invoke(this, new BookmarkChangeData()
  63. {
  64. PageIndex = bindData.BindProperty.PageIndex,
  65. BookmarkTitle = bindData.BindProperty.BookmarkTitle,
  66. BindData= bindData
  67. });
  68. }
  69. }
  70. }
  71. e.Handled = true;
  72. }
  73. private void DelBorder_Click(object sender, RoutedEventArgs e)
  74. {
  75. Border sourceBtn = sender as Border;
  76. if (sourceBtn != null)
  77. {
  78. DependencyObject findElement = null;
  79. if (FindParent<Grid>(sourceBtn, out findElement) && findElement != null)
  80. {
  81. Grid sourceGrid = findElement as Grid;
  82. BookmarkBindData bindData = sourceGrid.Tag as BookmarkBindData;
  83. if (bindData != null)
  84. {
  85. BookmarkDelete?.Invoke(this, new BookmarkChangeData()
  86. {
  87. PageIndex = bindData.BindProperty.PageIndex,
  88. BookmarkTitle = bindData.BindProperty.BookmarkTitle
  89. });
  90. bookmarkResults?.Remove(bindData);
  91. if(bookmarkResults.Count==0)
  92. {
  93. NoResultText.Visibility = Visibility.Visible;
  94. }
  95. }
  96. }
  97. }
  98. e.Handled = true;
  99. }
  100. private bool FindParent<T>(DependencyObject checkElement,out DependencyObject parent)
  101. {
  102. parent = null;
  103. try
  104. {
  105. if (checkElement != null)
  106. {
  107. DependencyObject parentElement= VisualTreeHelper.GetParent(checkElement);
  108. if(parentElement!=null && parentElement is T)
  109. {
  110. parent =parentElement;
  111. return true;
  112. }
  113. return FindParent<T>(parentElement,out parent);
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. }
  119. return false;
  120. }
  121. public void SetBookmarkResult(List<BindBookmarkResult> results)
  122. {
  123. bookmarkResults?.Clear();
  124. if (results == null || results.Count == 0)
  125. {
  126. ResultListControl.ItemsSource = null;
  127. NoResultText.Visibility= Visibility.Visible;
  128. return;
  129. }
  130. foreach (BindBookmarkResult item in results)
  131. {
  132. bookmarkResults.Add(new BookmarkBindData()
  133. {
  134. BindProperty = item
  135. });
  136. }
  137. ResultListControl.ItemsSource = bookmarkResults;
  138. NoResultText.Visibility = Visibility.Collapsed;
  139. }
  140. public BindBookmarkResult GetSelectItem()
  141. {
  142. BookmarkBindData bindData = ResultListControl.SelectedItem as BookmarkBindData;
  143. if (bindData != null)
  144. {
  145. return bindData.BindProperty;
  146. }
  147. return null;
  148. }
  149. public BindBookmarkResult GetItem(int index)
  150. {
  151. if (index < 0)
  152. {
  153. return null;
  154. }
  155. if (ResultListControl.HasItems && ResultListControl.Items.Count > index)
  156. {
  157. BookmarkBindData bindData = ResultListControl.Items[index] as BookmarkBindData;
  158. if (bindData != null)
  159. {
  160. return bindData.BindProperty;
  161. }
  162. }
  163. return null;
  164. }
  165. public void ClearSelection()
  166. {
  167. int oldSelectionIndex = ResultListControl.SelectedIndex;
  168. ResultListControl.UnselectAll();
  169. if (oldSelectionIndex != ResultListControl.SelectedIndex)
  170. {
  171. SelectionChanged?.Invoke(this,ResultListControl.SelectedIndex);
  172. }
  173. }
  174. public void SelectItem(int selectIndex)
  175. {
  176. if (ResultListControl.SelectedIndex != selectIndex)
  177. {
  178. ResultListControl.SelectedIndex = selectIndex;
  179. }
  180. }
  181. private void ResultListControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  182. {
  183. SelectionChanged?.Invoke(this, ResultListControl.SelectedIndex);
  184. }
  185. private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  186. {
  187. Grid sourceGrid = sender as Grid;
  188. if (sourceGrid != null)
  189. {
  190. BookmarkBindData bindData = sourceGrid.Tag as BookmarkBindData;
  191. if (bindData != null)
  192. {
  193. BookmarkClicked?.Invoke(this, bindData.BindProperty.PageIndex);
  194. }
  195. }
  196. }
  197. private void ResultListControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  198. {
  199. ResultListControl?.UnselectAll();
  200. }
  201. }
  202. public class BookmarkChangeData
  203. {
  204. public int PageIndex { get; set; }
  205. public string BookmarkTitle { get; set; }
  206. public string NewTitle { get;set; }
  207. public object BindData { get; set; }
  208. }
  209. public class BindBookmarkResult:INotifyPropertyChanged
  210. {
  211. private int _pageIndex;
  212. public int PageIndex
  213. {
  214. get
  215. {
  216. return _pageIndex;
  217. }
  218. set
  219. {
  220. if(_pageIndex != value)
  221. {
  222. _pageIndex = value;
  223. OnPropertyChanged(nameof(PageIndex));
  224. }
  225. }
  226. }
  227. private string _bookmarkTitle;
  228. public string BookmarkTitle
  229. {
  230. get
  231. {
  232. return _bookmarkTitle;
  233. }
  234. set
  235. {
  236. if(_bookmarkTitle != value)
  237. {
  238. _bookmarkTitle = value;
  239. OnPropertyChanged(nameof(BookmarkTitle));
  240. }
  241. }
  242. }
  243. public event PropertyChangedEventHandler PropertyChanged;
  244. protected void OnPropertyChanged(string propertyName)
  245. {
  246. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  247. }
  248. }
  249. internal class BookmarkBindData
  250. {
  251. public int ShowPageIndex { get { return BindProperty.PageIndex + 1; } set { BindProperty.PageIndex = value; } }
  252. public BindBookmarkResult BindProperty { get; set; }
  253. public BookmarkBindData()
  254. {
  255. BindProperty = new BindBookmarkResult();
  256. }
  257. }
  258. }