FileGridListControl.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. 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. dialog.Multiselect = true;
  76. foreach (var fileName in dialog.FileNames)
  77. {
  78. var file = new FileInfo(fileName);
  79. var fileInfoData = new FileInfoData()
  80. {
  81. FileName = file.Name,
  82. Size = GetSizeFromBytes(file.Length),
  83. Location = file.FullName
  84. };
  85. if (FileInfoDataList.All(item => item.Location != fileInfoData.Location))
  86. {
  87. FileInfoDataList.Add(fileInfoData);
  88. }
  89. }
  90. }
  91. private string GetSizeFromBytes(long bytes)
  92. {
  93. string[] sizes = { "B", "KB", "MB", "GB", "TB" };
  94. double len = bytes;
  95. int order = 0;
  96. while (len >= 1024 && order < sizes.Length - 1)
  97. {
  98. order++;
  99. len /= 1024;
  100. }
  101. return $"{len:0.##} {sizes[order]}";
  102. }
  103. private void Remove_Click(object sender, RoutedEventArgs e)
  104. {
  105. if (FileDataGrid.SelectedItems.Count == 0)
  106. {
  107. FileInfoDataList.Clear();
  108. OnPropertyChanged("FileNum");
  109. return;
  110. }
  111. var selectedItems = FileDataGrid.SelectedItems;
  112. var selectedItemsList = selectedItems.Cast<FileInfoData>().ToList();
  113. foreach (var item in selectedItemsList)
  114. {
  115. FileInfoDataList.Remove(item);
  116. }
  117. OnPropertyChanged("FileNum");
  118. }
  119. protected virtual void OnPropertyChanged(string propertyName = null)
  120. {
  121. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  122. }
  123. protected bool UpdateProper<T>(ref T properValue,
  124. T newValue,
  125. [CallerMemberName] string properName = "")
  126. {
  127. if (object.Equals(properValue, newValue))
  128. return false;
  129. properValue = newValue;
  130. OnPropertyChanged(properName);
  131. return true;
  132. }
  133. private void FileDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  134. {
  135. BtnRemove.Content = FileDataGrid.SelectedItems.Count > 0 ? "Remove" : "Remove All";
  136. }
  137. private void FileDataGrid_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  138. {
  139. if (e.OriginalSource.GetType() != typeof(DataGridCell))
  140. {
  141. FileDataGrid.UnselectAll();
  142. }
  143. }
  144. }
  145. }