FileSelectedVM.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.Collections.ObjectModel;
  2. using System.ComponentModel;
  3. using System.IO;
  4. using System.Windows.Input;
  5. namespace WatchDog.WPF
  6. {
  7. public class FileSelectedVM : INotifyPropertyChanged
  8. {
  9. private string selectedItem;
  10. public string SelectedItem
  11. {
  12. get { return selectedItem; }
  13. set
  14. {
  15. selectedItem = value;
  16. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItem)));
  17. if (!string.IsNullOrEmpty(selectedItem))
  18. {
  19. CurrentDir = selectedItem;
  20. }
  21. }
  22. }
  23. private string currentDir = @"C:\Users\77411\Desktop";
  24. public string CurrentDir
  25. {
  26. get { return currentDir; }
  27. set
  28. {
  29. currentDir = value;
  30. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentDir)));
  31. }
  32. }
  33. public ICommand ExpandDirectory { get; }
  34. public ICommand ExpandCurrentDirectory { get; }
  35. public ICommand PreDirectory { get; }
  36. public ICommand RootDirectory { get; }
  37. public ICommand SelectedDirectory { get; }
  38. public ObservableCollection<string> FilesPath { get; set; } = new ObservableCollection<string>();
  39. public FileSelectedVM()
  40. {
  41. SelectedDirectory = new AnotherCommandImplementation(_ =>
  42. {
  43. if (!string.IsNullOrEmpty(selectedItem))
  44. {
  45. //dataBus.SetDirPath(SelectedItem);
  46. }
  47. else
  48. {
  49. //dataBus.SetDirPath(CurrentDir);
  50. }
  51. });
  52. ExpandDirectory = new AnotherCommandImplementation(_ =>
  53. {
  54. if (string.IsNullOrWhiteSpace(SelectedItem))
  55. {
  56. return;
  57. }
  58. var dirs = Directory.GetDirectories(SelectedItem);
  59. FilesPath.Clear();
  60. foreach (var item in dirs)
  61. {
  62. FilesPath.Add(item);
  63. }
  64. });
  65. PreDirectory = new AnotherCommandImplementation(_ =>
  66. {
  67. if (string.IsNullOrWhiteSpace(CurrentDir))
  68. {
  69. RootDirectory.Execute(_);
  70. return;
  71. }
  72. var root = Directory.GetDirectoryRoot(CurrentDir);
  73. if (root == CurrentDir)
  74. {
  75. RootDirectory.Execute(_);
  76. return;
  77. }
  78. FilesPath.Clear();
  79. var dir = Directory.GetParent(CurrentDir);
  80. CurrentDir = dir.FullName;
  81. var dirInfos = dir.GetDirectories();
  82. foreach (var item in dirInfos)
  83. {
  84. FilesPath.Add(item.FullName);
  85. }
  86. });
  87. ExpandCurrentDirectory = new AnotherCommandImplementation(_ => {
  88. var dirs = Directory.GetDirectories(CurrentDir);
  89. FilesPath.Clear();
  90. foreach (var item in dirs)
  91. {
  92. FilesPath.Add(item);
  93. }
  94. });
  95. RootDirectory =
  96. new AnotherCommandImplementation(_ =>
  97. {
  98. FilesPath.Clear();
  99. DriveInfo[] allDirves = DriveInfo.GetDrives();
  100. foreach (var item in allDirves)
  101. {
  102. FilesPath.Add(@item.Name);
  103. }
  104. });
  105. }
  106. public event PropertyChangedEventHandler PropertyChanged;
  107. }
  108. }