ImportFilePopupViewModel.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Microsoft.Win32;
  2. using PixiEditor.Helpers;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Media.Imaging;
  10. namespace PixiEditor.ViewModels
  11. {
  12. class ImportFilePopupViewModel : ViewModelBase
  13. {
  14. public RelayCommand CloseButtonCommand { get; set; }
  15. public RelayCommand DragMoveCommand { get; set; }
  16. public RelayCommand ChoosePathCommand { get; set; }
  17. public RelayCommand OkCommand { get; set; }
  18. private string _pathButtonBorder = "#f08080";
  19. public string PathButtonBorder
  20. {
  21. get { return _pathButtonBorder; }
  22. set { if (_pathButtonBorder != value) { _pathButtonBorder = value; RaisePropertyChanged("PathButtonBorder"); } }
  23. }
  24. private bool _pathIsCorrect;
  25. public bool PathIsCorrect
  26. {
  27. get { return _pathIsCorrect; }
  28. set { if (_pathIsCorrect != value) { _pathIsCorrect = value; RaisePropertyChanged("PathIsCorrect"); } }
  29. }
  30. private string _filePath;
  31. public string FilePath
  32. {
  33. get { return _filePath; }
  34. set { if (_filePath != value) { _filePath = value; RaisePropertyChanged("FilePath"); } }
  35. }
  36. private int _importWidth = 16;
  37. public int ImportWidth
  38. {
  39. get { return _importWidth; }
  40. set { if (_importWidth != value) { _importWidth = value; RaisePropertyChanged("ImportWidth"); } }
  41. }
  42. private int _importHeight = 16;
  43. public int ImportHeight
  44. {
  45. get { return _importHeight; }
  46. set { if (_importHeight != value) { _importHeight = value; RaisePropertyChanged("ImportHeight"); } }
  47. }
  48. public ImportFilePopupViewModel()
  49. {
  50. CloseButtonCommand = new RelayCommand(CloseWindow);
  51. DragMoveCommand = new RelayCommand(MoveWindow);
  52. ChoosePathCommand = new RelayCommand(ChoosePath);
  53. OkCommand = new RelayCommand(OkButton, CanClickOk);
  54. }
  55. /// <summary>
  56. /// Command that handles Path choosing to save file
  57. /// </summary>
  58. /// <param name="parameter"></param>
  59. private void ChoosePath(object parameter)
  60. {
  61. OpenFileDialog path = new OpenFileDialog()
  62. {
  63. Title = "Import path",
  64. CheckPathExists = true,
  65. Filter = "Image Files|*.png;*.jpeg;*.jpg"
  66. };
  67. if (path.ShowDialog() == true)
  68. {
  69. if (string.IsNullOrEmpty(path.FileName) == false)
  70. {
  71. PathButtonBorder = "#b8f080";
  72. PathIsCorrect = true;
  73. FilePath = path.FileName;
  74. BitmapImage bitmap = new BitmapImage(new Uri(path.FileName));
  75. ImportHeight = (int)bitmap.Height;
  76. ImportWidth = (int)bitmap.Width;
  77. }
  78. else
  79. {
  80. PathButtonBorder = "#f08080";
  81. PathIsCorrect = false;
  82. }
  83. }
  84. }
  85. private void CloseWindow(object parameter)
  86. {
  87. ((Window)parameter).DialogResult = false;
  88. base.CloseButton(parameter);
  89. }
  90. private void MoveWindow(object parameter)
  91. {
  92. base.DragMove(parameter);
  93. }
  94. private void OkButton(object parameter)
  95. {
  96. ((Window)parameter).DialogResult = true;
  97. base.CloseButton(parameter);
  98. }
  99. private bool CanClickOk(object property)
  100. {
  101. return PathIsCorrect == true;
  102. }
  103. }
  104. }