SaveFilePopupViewModel.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Microsoft.Win32;
  2. using PixiEditor.Helpers;
  3. using System.Windows;
  4. namespace PixiEditor.ViewModels
  5. {
  6. internal class SaveFilePopupViewModel : ViewModelBase
  7. {
  8. private string _filePath;
  9. public SaveFilePopupViewModel()
  10. {
  11. CloseButtonCommand = new RelayCommand(CloseWindow);
  12. DragMoveCommand = new RelayCommand(MoveWindow);
  13. OkCommand = new RelayCommand(OkButton);
  14. }
  15. public RelayCommand CloseButtonCommand { get; set; }
  16. public RelayCommand DragMoveCommand { get; set; }
  17. public RelayCommand OkCommand { get; set; }
  18. public string FilePath
  19. {
  20. get => _filePath;
  21. set
  22. {
  23. if (_filePath != value)
  24. {
  25. _filePath = value;
  26. RaisePropertyChanged("FilePath");
  27. }
  28. }
  29. }
  30. /// <summary>
  31. /// Command that handles Path choosing to save file
  32. /// </summary>
  33. private string ChoosePath()
  34. {
  35. SaveFileDialog path = new SaveFileDialog
  36. {
  37. Title = "Export path",
  38. CheckPathExists = true,
  39. DefaultExt = "PNG Image (.png) | *.png",
  40. Filter = "PNG Image (.png) | *.png"
  41. };
  42. if (path.ShowDialog() == true)
  43. {
  44. if (string.IsNullOrEmpty(path.FileName) == false)
  45. {
  46. return path.FileName;
  47. }
  48. }
  49. return null;
  50. }
  51. private void CloseWindow(object parameter)
  52. {
  53. ((Window)parameter).DialogResult = false;
  54. CloseButton(parameter);
  55. }
  56. private void MoveWindow(object parameter)
  57. {
  58. DragMove(parameter);
  59. }
  60. private void OkButton(object parameter)
  61. {
  62. string path = ChoosePath();
  63. if (path == null)
  64. return;
  65. FilePath = path;
  66. ((Window)parameter).DialogResult = true;
  67. CloseButton(parameter);
  68. }
  69. }
  70. }