FileGridListControl.xaml.cs 3.4 KB

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