FileGridListControl.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.Runtime.CompilerServices;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Input;
  12. using Microsoft.Win32;
  13. namespace Compdfkit_Tools.PDFControl
  14. {
  15. public partial class FileGridListControl : UserControl, INotifyPropertyChanged
  16. {
  17. public static readonly DependencyProperty IsEnsureProperty =
  18. DependencyProperty.Register(nameof(IsEnsure), typeof(bool), typeof(FileGridListControl), new PropertyMetadata(false));
  19. public bool IsEnsure
  20. {
  21. get => (bool)GetValue(IsEnsureProperty);
  22. private set
  23. {
  24. SetValue(IsEnsureProperty, value);
  25. }
  26. }
  27. public class FileInfoData
  28. {
  29. public string FileName { get; set; }
  30. public string Size { get; set; }
  31. public string Location { get; set; }
  32. };
  33. private int _fileNumText;
  34. public int FileNumText
  35. {
  36. get => _fileNumText;
  37. set
  38. {
  39. if(UpdateProper(ref _fileNumText, value))
  40. IsEnsure = _fileNumText > 0;
  41. }
  42. }
  43. public event PropertyChangedEventHandler PropertyChanged;
  44. public event EventHandler FileNumChanged;
  45. private ObservableCollection<FileInfoData> _fileInfoDataList;
  46. public ObservableCollection<FileInfoData> FileInfoDataList
  47. {
  48. get
  49. {
  50. return _fileInfoDataList;
  51. }
  52. set
  53. {
  54. _fileInfoDataList = value;
  55. _fileInfoDataList.CollectionChanged += (sender, args) =>
  56. {
  57. FileNumText = _fileInfoDataList.Count;
  58. };
  59. }
  60. }
  61. public FileGridListControl()
  62. {
  63. InitializeComponent();
  64. FileInfoDataList = new ObservableCollection<FileInfoData>();
  65. DataContext = this;
  66. FileDataGrid.ItemsSource = FileInfoDataList;
  67. FileInfoDataList.Clear();
  68. }
  69. private void AddFiles_Click(object sender, RoutedEventArgs e)
  70. {
  71. var dialog = new OpenFileDialog();
  72. dialog.Multiselect = true;
  73. dialog.Filter = @"PDF Files (*.pdf)|*.pdf";
  74. dialog.ShowDialog();
  75. foreach (var fileName in dialog.FileNames)
  76. {
  77. var file = new FileInfo(fileName);
  78. var fileInfoData = new FileInfoData()
  79. {
  80. FileName = file.Name,
  81. Size = GetSizeFromBytes(file.Length),
  82. Location = file.FullName
  83. };
  84. if (FileInfoDataList.All(item => item.Location != fileInfoData.Location))
  85. {
  86. FileInfoDataList.Add(fileInfoData);
  87. }
  88. }
  89. }
  90. private string GetSizeFromBytes(long bytes)
  91. {
  92. string[] sizes = { "B", "KB", "MB", "GB", "TB" };
  93. double len = bytes;
  94. int order = 0;
  95. while (len >= 1024 && order < sizes.Length - 1)
  96. {
  97. order++;
  98. len /= 1024;
  99. }
  100. return $"{len:0.##} {sizes[order]}";
  101. }
  102. private void Remove_Click(object sender, RoutedEventArgs e)
  103. {
  104. if (FileDataGrid.SelectedItems.Count == 0)
  105. {
  106. FileInfoDataList.Clear();
  107. OnPropertyChanged("FileNum");
  108. return;
  109. }
  110. var selectedItems = FileDataGrid.SelectedItems;
  111. var selectedItemsList = selectedItems.Cast<FileInfoData>().ToList();
  112. foreach (var item in selectedItemsList)
  113. {
  114. FileInfoDataList.Remove(item);
  115. }
  116. OnPropertyChanged("FileNum");
  117. }
  118. protected virtual void OnPropertyChanged(string propertyName = null)
  119. {
  120. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  121. }
  122. protected bool UpdateProper<T>(ref T properValue,
  123. T newValue,
  124. [CallerMemberName] string properName = "")
  125. {
  126. if (object.Equals(properValue, newValue))
  127. return false;
  128. properValue = newValue;
  129. OnPropertyChanged(properName);
  130. return true;
  131. }
  132. private void FileDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  133. {
  134. BtnRemove.Content = FileDataGrid.SelectedItems.Count > 0 ? "Remove" : "Remove All";
  135. }
  136. private void FileDataGrid_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  137. {
  138. if (e.OriginalSource.GetType() != typeof(DataGridCell))
  139. {
  140. FileDataGrid.UnselectAll();
  141. }
  142. }
  143. }
  144. }