FileGridListControl.xaml.cs 4.1 KB

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