EditWindow.xaml.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Microsoft.Win32;
  2. using mysql.Classes;
  3. using mysql.Model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. namespace mysql.Windows
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для EditWindow.xaml
  22. /// </summary>
  23. public partial class EditWindow : Window, INotifyPropertyChanged
  24. {
  25. public Product CurrentProduct { get; set; }
  26. public IEnumerable<ProductType> ProductTypes { get; set; }
  27. public EditWindow(Product EditProduct)
  28. {
  29. InitializeComponent();
  30. DataContext = this;
  31. CurrentProduct = EditProduct;
  32. ProductTypes = Globals.DataProvider.GetProductTypes();
  33. }
  34. public string WindowName
  35. {
  36. get
  37. {
  38. return CurrentProduct.ID == 0 ? "Новый продукт" : "Редактирование продукта";
  39. }
  40. }
  41. public event PropertyChangedEventHandler PropertyChanged;
  42. private void ChangeImage_Click(object sender, RoutedEventArgs e)
  43. {
  44. OpenFileDialog GetImageDialog = new OpenFileDialog();
  45. // задаем фильтр для выбираемых файлов
  46. // до символа "|" идет произвольный текст, а после него шаблоны файлов раздеренные точкой с запятой
  47. GetImageDialog.Filter = "Файлы изображений: (*.png, *.jpg)|*.png;*.jpg";
  48. // чтобы не искать по всему диску задаем начальный каталог
  49. GetImageDialog.InitialDirectory = Environment.CurrentDirectory;
  50. if (GetImageDialog.ShowDialog() == true)
  51. {
  52. // перед присвоением пути к картинке обрезаем начало строки, т.к. диалог возвращает полный путь
  53. // (тут конечно еще надо проверить есть ли в начале Environment.CurrentDirectory)
  54. CurrentProduct.Image = GetImageDialog.FileName.Substring(Environment.CurrentDirectory.Length);
  55. Invalidate();
  56. }
  57. }
  58. private void Invalidate(string ComponentName = "CurrentProduct")
  59. {
  60. if (PropertyChanged != null)
  61. PropertyChanged(this, new PropertyChangedEventArgs(ComponentName));
  62. }
  63. private void Button_Click(object sender, RoutedEventArgs e)
  64. {
  65. try
  66. {
  67. Globals.DataProvider.SaveProduct(CurrentProduct);
  68. DialogResult = true;
  69. }
  70. catch (Exception ex)
  71. {
  72. MessageBox.Show(ex.Message);
  73. }
  74. }
  75. }
  76. }