12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using Microsoft.Win32;
- namespace Compdfkit_Tools.PDFControl
- {
- public partial class EncryptionFileListControl : UserControl, INotifyPropertyChanged
- {
- public class FileInfoData
- {
- public string FileName { get; set; }
- public string Size { get; set; }
- public string Location { get; set; }
- };
-
- public event PropertyChangedEventHandler PropertyChanged;
- private ObservableCollection<FileInfoData> _fileInfoDataList = new ObservableCollection<FileInfoData>();
-
- public int FileNum => _fileInfoDataList.Count;
- public EncryptionFileListControl()
- {
- InitializeComponent();
- DataContext = this;
- FileDataGrid.ItemsSource = _fileInfoDataList;
- }
-
- private void AddFiles_Click(object sender, RoutedEventArgs e)
- {
- var dialog = new OpenFileDialog();
- dialog.Multiselect = true;
- dialog.Filter = @"PDF Files (*.pdf)|*.pdf";
- dialog.ShowDialog();
- dialog.Multiselect = true;
- foreach (var fileName in dialog.FileNames)
- {
- var file = new FileInfo(fileName);
- var fileInfoData = new FileInfoData()
- {
- FileName = file.Name,
- Size = GetSizeFromBytes(file.Length),
- Location = file.FullName
- };
-
- if (_fileInfoDataList.All(item => item.Location != fileInfoData.Location))
- {
- _fileInfoDataList.Add(fileInfoData);
- }
- }
- OnPropertyChanged("FileNum");
- }
-
- private string GetSizeFromBytes(long bytes)
- {
- string[] sizes = { "B", "KB", "MB", "GB", "TB" };
- double len = bytes;
- int order = 0;
- while (len >= 1024 && order < sizes.Length - 1)
- {
- order++;
- len /= 1024;
- }
- return $"{len:0.##} {sizes[order]}";
- }
- private void Remove_Click(object sender, RoutedEventArgs e)
- {
- var selectedItems = FileDataGrid.SelectedItems;
- var selectedItemsList = selectedItems.Cast<FileInfoData>().ToList();
- foreach (var item in selectedItemsList)
- {
- _fileInfoDataList.Remove(item);
- }
- OnPropertyChanged("FileNum");
- }
-
- protected virtual void OnPropertyChanged(string propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
|