FeaturesListControl.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using ComPDFKit.Controls.Helper;
  19. namespace ComPDFKit.Controls.PDFControl
  20. {
  21. /// <summary>
  22. /// Interaction logic for FeaturesListControl.xaml
  23. /// </summary>
  24. public partial class FeaturesListControl : UserControl, INotifyPropertyChanged
  25. {
  26. private int _currentIndex = 1;
  27. public int CurrentIndex
  28. {
  29. get => _currentIndex;
  30. set
  31. {
  32. if (UpdateProper(ref _currentIndex, value))
  33. {
  34. RefreshCurrentItems();
  35. }
  36. }
  37. }
  38. private int _totalIndex = 1;
  39. public int TotalIndex
  40. {
  41. get => _totalIndex;
  42. set
  43. {
  44. UpdateProper(ref _totalIndex, value);
  45. }
  46. }
  47. private int _horizontalItemNumber = 1;
  48. public int HorizontalItemNumber
  49. {
  50. get => _horizontalItemNumber;
  51. set
  52. {
  53. if (UpdateProper(ref _horizontalItemNumber, value))
  54. {
  55. ResetTotalIndex();
  56. }
  57. }
  58. }
  59. private int _verticalItemNumber = 1;
  60. public int VerticalItemNumber
  61. {
  62. get => _verticalItemNumber;
  63. set
  64. {
  65. if (UpdateProper(ref _verticalItemNumber, value))
  66. {
  67. ResetTotalIndex();
  68. }
  69. }
  70. }
  71. private ObservableCollection<CustomItem> _pageItems = new ObservableCollection<CustomItem>();
  72. public ObservableCollection<CustomItem> PageItems
  73. {
  74. get => _pageItems;
  75. set
  76. {
  77. UpdateProper(ref _pageItems, value);
  78. }
  79. }
  80. public event EventHandler<CustomItem> SelectionChanged;
  81. public event PropertyChangedEventHandler PropertyChanged;
  82. public static readonly DependencyProperty ItemsProperty =
  83. DependencyProperty.Register("Items", typeof(ObservableCollection<CustomItem>), typeof(FeaturesListControl), new PropertyMetadata(null));
  84. public ObservableCollection<CustomItem> Items
  85. {
  86. get { return (ObservableCollection<CustomItem>)GetValue(ItemsProperty); }
  87. set { SetValue(ItemsProperty, value); }
  88. }
  89. public FeaturesListControl()
  90. {
  91. this.DataContext = this;
  92. InitializeComponent();
  93. Items = new ObservableCollection<CustomItem>();
  94. FeaturesListTxb.Text = LanguageHelper.CommonManager.GetString("Title_Features");
  95. }
  96. private void FeaturesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  97. {
  98. var listBox = sender as ListBox;
  99. if (listBox != null && listBox.SelectedIndex != -1)
  100. {
  101. SelectionChanged?.Invoke(sender, Items[HorizontalItemNumber*VerticalItemNumber*(CurrentIndex-1) + listBox.SelectedIndex]);
  102. listBox.SelectedIndex = -1;
  103. }
  104. }
  105. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  106. {
  107. FeaturesListBox.SelectionChanged += FeaturesListBox_SelectionChanged;
  108. ResetTotalIndex();
  109. }
  110. private void ResetTotalIndex()
  111. {
  112. if (this.IsLoaded)
  113. {
  114. if (FeaturesListBox != null && HorizontalItemNumber != 0 && VerticalItemNumber != 0)
  115. {
  116. int totalIndex = Items.Count / (HorizontalItemNumber * VerticalItemNumber);
  117. if (Items.Count % (HorizontalItemNumber * VerticalItemNumber) != 0)
  118. {
  119. TotalIndex = totalIndex + 1;
  120. }
  121. else
  122. {
  123. TotalIndex = totalIndex;
  124. }
  125. if (CurrentIndex > TotalIndex)
  126. {
  127. CurrentIndex = TotalIndex;
  128. }
  129. RefreshCurrentItems();
  130. }
  131. }
  132. }
  133. private void RefreshCurrentItems()
  134. {
  135. if (this.IsLoaded)
  136. {
  137. PageItems.Clear();
  138. int baseItemIndex = (CurrentIndex - 1) * HorizontalItemNumber * VerticalItemNumber;
  139. for (int ItemIndex = baseItemIndex;
  140. ItemIndex < baseItemIndex + HorizontalItemNumber * VerticalItemNumber && ItemIndex < Items.Count;
  141. ItemIndex++)
  142. {
  143. PageItems.Add(Items[ItemIndex]);
  144. }
  145. }
  146. }
  147. private void PrePageBtn_Click(object sender, RoutedEventArgs e)
  148. {
  149. if (CurrentIndex > 1)
  150. {
  151. CurrentIndex--;
  152. }
  153. }
  154. private void NextPageBtn_Click(object sender, RoutedEventArgs e)
  155. {
  156. if (CurrentIndex < TotalIndex)
  157. {
  158. CurrentIndex++;
  159. }
  160. }
  161. protected bool UpdateProper<T>(ref T properValue,
  162. T newValue,
  163. [CallerMemberName] string properName = "")
  164. {
  165. if (object.Equals(properValue, newValue))
  166. return false;
  167. properValue = newValue;
  168. OnPropertyChanged(properName);
  169. return true;
  170. }
  171. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  172. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  173. private void ListGd_SizeChanged(object sender, SizeChangedEventArgs e)
  174. {
  175. if (FeaturesListBox != null && CustomItem.ItemWidth > 0 && CustomItem.ItemHeight > 0)
  176. {
  177. HorizontalItemNumber = (int)((FeaturesListBox.ActualWidth) / (CustomItem.ItemWidth + 2 * CustomItem.ItemMargin));
  178. VerticalItemNumber = (int)((FeaturesListBox.ActualHeight) / (CustomItem.ItemHeight + 2 * CustomItem.ItemMargin));
  179. }
  180. }
  181. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  182. {
  183. FeaturesListBox.SelectionChanged -= FeaturesListBox_SelectionChanged;
  184. }
  185. }
  186. public class CustomItem : Control, INotifyPropertyChanged
  187. {
  188. static public double ItemWidth { get; set; } = 400;
  189. static public double ItemHeight { get; set; } = 128;
  190. static public double ItemMargin { get; set; } = 10;
  191. public string Feature;
  192. private string _titleText;
  193. public string TitleText
  194. {
  195. get => _titleText;
  196. set
  197. {
  198. UpdateProper(ref _titleText, value);
  199. }
  200. }
  201. private Canvas _iconCanvas;
  202. public Canvas IconCanvas
  203. {
  204. get => _iconCanvas;
  205. set
  206. {
  207. UpdateProper(ref _iconCanvas, value);
  208. }
  209. }
  210. private string _descriptionText;
  211. public string DescriptionText
  212. {
  213. get => _descriptionText;
  214. set
  215. {
  216. UpdateProper(ref _descriptionText, value);
  217. }
  218. }
  219. public event PropertyChangedEventHandler PropertyChanged;
  220. protected void UpdateProper<T>(ref T properValue,
  221. T newValue,
  222. [CallerMemberName] string properName = "")
  223. {
  224. if (object.Equals(properValue, newValue))
  225. return;
  226. properValue = newValue;
  227. OnPropertyChanged(properName);
  228. }
  229. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  230. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  231. }
  232. }