FileGridListControl.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Input;
  13. using Compdfkit_Tools.Common;
  14. using Compdfkit_Tools.Helper;
  15. using ComPDFKit.PDFDocument;
  16. using Microsoft.Win32;
  17. namespace Compdfkit_Tools.PDFControl
  18. {
  19. public partial class FileGridListControl : UserControl, INotifyPropertyChanged
  20. {
  21. public static readonly DependencyProperty IsEnsureProperty =
  22. DependencyProperty.Register(nameof(IsEnsure), typeof(bool), typeof(FileGridListControl), new PropertyMetadata(false));
  23. public bool IsEnsure
  24. {
  25. get => (bool)GetValue(IsEnsureProperty);
  26. private set
  27. {
  28. SetValue(IsEnsureProperty, value);
  29. }
  30. }
  31. public class FileInfoData
  32. {
  33. public string FileName { get; set; }
  34. public string Size { get; set; }
  35. public string Location { get; set; }
  36. public CPDFDocument Document { get; set; }
  37. };
  38. private int _fileNumText;
  39. public int FileNumText
  40. {
  41. get => _fileNumText;
  42. set
  43. {
  44. if (UpdateProper(ref _fileNumText, value))
  45. IsEnsure = _fileNumText > 0;
  46. }
  47. }
  48. public event PropertyChangedEventHandler PropertyChanged;
  49. private ObservableCollection<FileInfoData> _fileInfoDataList;
  50. public ObservableCollection<FileInfoData> FileInfoDataList
  51. {
  52. get
  53. {
  54. return _fileInfoDataList;
  55. }
  56. set
  57. {
  58. _fileInfoDataList = value;
  59. _fileInfoDataList.CollectionChanged += (sender, args) =>
  60. {
  61. FileNumText = _fileInfoDataList.Count;
  62. };
  63. }
  64. }
  65. public FileGridListControl()
  66. {
  67. InitializeComponent();
  68. FileInfoDataList = new ObservableCollection<FileInfoData>();
  69. DataContext = this;
  70. FileDataGrid.ItemsSource = FileInfoDataList;
  71. FileInfoDataList.Clear();
  72. }
  73. private void AddFiles_Click(object sender, RoutedEventArgs e)
  74. {
  75. var dialog = new OpenFileDialog();
  76. dialog.Multiselect = true;
  77. dialog.Filter = @"PDF Files (*.pdf)|*.pdf";
  78. dialog.ShowDialog();
  79. foreach (var fileName in dialog.FileNames)
  80. {
  81. var file = new FileInfo(fileName);
  82. var fileInfoData = new FileInfoData()
  83. {
  84. FileName = file.Name,
  85. Size = CommonHelper.GetFileSize(fileName),
  86. Location = file.FullName
  87. };
  88. if (FileInfoDataList.Any(item => item.Location == fileInfoData.Location))
  89. {
  90. continue;
  91. }
  92. var document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
  93. if (document.IsLocked)
  94. {
  95. PasswordWindow window = new PasswordWindow();
  96. window.InitWithDocument(document);
  97. window.Owner = Window.GetWindow(this);
  98. window.PasswordDialog.SetShowText(fileInfoData.FileName + " is encrypted.");
  99. window.ShowDialog();
  100. if (document.IsLocked)
  101. {
  102. continue;
  103. }
  104. }
  105. fileInfoData.Document = document;
  106. FileInfoDataList.Add(fileInfoData);
  107. }
  108. }
  109. private void Remove_Click(object sender, RoutedEventArgs e)
  110. {
  111. if (FileDataGrid.SelectedItems.Count == 0)
  112. {
  113. FileInfoDataList.Clear();
  114. OnPropertyChanged("FileNum");
  115. return;
  116. }
  117. var selectedItems = FileDataGrid.SelectedItems;
  118. var selectedItemsList = selectedItems.Cast<FileInfoData>().ToList();
  119. foreach (var item in selectedItemsList)
  120. {
  121. FileInfoDataList.Remove(item);
  122. }
  123. OnPropertyChanged("FileNum");
  124. }
  125. protected virtual void OnPropertyChanged(string propertyName = null)
  126. {
  127. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  128. }
  129. protected bool UpdateProper<T>(ref T properValue,
  130. T newValue,
  131. [CallerMemberName] string properName = "")
  132. {
  133. if (object.Equals(properValue, newValue))
  134. return false;
  135. properValue = newValue;
  136. OnPropertyChanged(properName);
  137. return true;
  138. }
  139. private void FileDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  140. {
  141. BtnRemove.Content = FileDataGrid.SelectedItems.Count > 0 ? "Remove" : "Remove All";
  142. }
  143. private void FileDataGrid_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  144. {
  145. FileDataGrid.UnselectAll();
  146. }
  147. }
  148. }