FileGridListControl.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Compdfkit_Tools.Helper;
  13. using Microsoft.Win32;
  14. namespace Compdfkit_Tools.PDFControl
  15. {
  16. public partial class FileGridListControl : UserControl, INotifyPropertyChanged
  17. {
  18. public static readonly DependencyProperty IsEnsureProperty =
  19. DependencyProperty.Register(nameof(IsEnsure), typeof(bool), typeof(FileGridListControl), new PropertyMetadata(false));
  20. public bool IsEnsure
  21. {
  22. get => (bool)GetValue(IsEnsureProperty);
  23. private set
  24. {
  25. SetValue(IsEnsureProperty, value);
  26. }
  27. }
  28. public class FileInfoData
  29. {
  30. public string FileName { get; set; }
  31. public string Size { get; set; }
  32. public string Location { get; set; }
  33. };
  34. private int _fileNumText;
  35. public int FileNumText
  36. {
  37. get => _fileNumText;
  38. set
  39. {
  40. if(UpdateProper(ref _fileNumText, value))
  41. IsEnsure = _fileNumText > 0;
  42. }
  43. }
  44. public event PropertyChangedEventHandler PropertyChanged;
  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 = CommonHelper.GetFileSize(fileName),
  82. Location = file.FullName
  83. };
  84. if (FileInfoDataList.All(item => item.Location != fileInfoData.Location))
  85. {
  86. FileInfoDataList.Add(fileInfoData);
  87. }
  88. }
  89. }
  90. private void Remove_Click(object sender, RoutedEventArgs e)
  91. {
  92. if (FileDataGrid.SelectedItems.Count == 0)
  93. {
  94. FileInfoDataList.Clear();
  95. OnPropertyChanged("FileNum");
  96. return;
  97. }
  98. var selectedItems = FileDataGrid.SelectedItems;
  99. var selectedItemsList = selectedItems.Cast<FileInfoData>().ToList();
  100. foreach (var item in selectedItemsList)
  101. {
  102. FileInfoDataList.Remove(item);
  103. }
  104. OnPropertyChanged("FileNum");
  105. }
  106. protected virtual void OnPropertyChanged(string propertyName = null)
  107. {
  108. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  109. }
  110. protected bool UpdateProper<T>(ref T properValue,
  111. T newValue,
  112. [CallerMemberName] string properName = "")
  113. {
  114. if (object.Equals(properValue, newValue))
  115. return false;
  116. properValue = newValue;
  117. OnPropertyChanged(properName);
  118. return true;
  119. }
  120. private void FileDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  121. {
  122. BtnRemove.Content = FileDataGrid.SelectedItems.Count > 0 ? "Remove" : "Remove All";
  123. }
  124. private void FileDataGrid_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  125. {
  126. if (e.OriginalSource.GetType() != typeof(DataGridCell))
  127. {
  128. FileDataGrid.UnselectAll();
  129. }
  130. }
  131. }
  132. }