EncryptionFileListControl.xaml.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.ObjectModel;
  2. using System.ComponentModel;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using Microsoft.Win32;
  8. namespace Compdfkit_Tools.PDFControl
  9. {
  10. public partial class EncryptionFileListControl : UserControl, INotifyPropertyChanged
  11. {
  12. public class FileInfoData
  13. {
  14. public string FileName { get; set; }
  15. public string Size { get; set; }
  16. public string Location { get; set; }
  17. };
  18. public event PropertyChangedEventHandler PropertyChanged;
  19. private ObservableCollection<FileInfoData> _fileInfoDataList = new ObservableCollection<FileInfoData>();
  20. public int FileNum => _fileInfoDataList.Count;
  21. public EncryptionFileListControl()
  22. {
  23. InitializeComponent();
  24. DataContext = this;
  25. FileDataGrid.ItemsSource = _fileInfoDataList;
  26. }
  27. private void AddFiles_Click(object sender, RoutedEventArgs e)
  28. {
  29. var dialog = new OpenFileDialog();
  30. dialog.Multiselect = true;
  31. dialog.Filter = @"PDF Files (*.pdf)|*.pdf";
  32. dialog.ShowDialog();
  33. dialog.Multiselect = true;
  34. foreach (var fileName in dialog.FileNames)
  35. {
  36. var file = new FileInfo(fileName);
  37. var fileInfoData = new FileInfoData()
  38. {
  39. FileName = file.Name,
  40. Size = GetSizeFromBytes(file.Length),
  41. Location = file.FullName
  42. };
  43. if (_fileInfoDataList.All(item => item.Location != fileInfoData.Location))
  44. {
  45. _fileInfoDataList.Add(fileInfoData);
  46. }
  47. }
  48. OnPropertyChanged("FileNum");
  49. }
  50. private string GetSizeFromBytes(long bytes)
  51. {
  52. string[] sizes = { "B", "KB", "MB", "GB", "TB" };
  53. double len = bytes;
  54. int order = 0;
  55. while (len >= 1024 && order < sizes.Length - 1)
  56. {
  57. order++;
  58. len /= 1024;
  59. }
  60. return $"{len:0.##} {sizes[order]}";
  61. }
  62. private void Remove_Click(object sender, RoutedEventArgs e)
  63. {
  64. var selectedItems = FileDataGrid.SelectedItems;
  65. var selectedItemsList = selectedItems.Cast<FileInfoData>().ToList();
  66. foreach (var item in selectedItemsList)
  67. {
  68. _fileInfoDataList.Remove(item);
  69. }
  70. OnPropertyChanged("FileNum");
  71. }
  72. protected virtual void OnPropertyChanged(string propertyName = null)
  73. {
  74. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  75. }
  76. }
  77. }