pageEditServiseImage.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace school
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для pageEditServiseImage.xaml
  21. /// </summary>
  22. public partial class pageEditServiseImage : Page
  23. {
  24. Service thisService;
  25. private void convertImage(byte[] byteArray, System.Windows.Controls.Image img)
  26. {
  27. BitmapImage BI = new BitmapImage();
  28. BI.BeginInit();
  29. BI.StreamSource = new MemoryStream(byteArray);
  30. BI.EndInit();
  31. img.Source = BI;
  32. img.Stretch = Stretch.Uniform;
  33. }
  34. public pageEditServiseImage(int idServise)
  35. {
  36. thisService = baseConnect.thisBase.Service.Where(tb => tb.ID == idServise).FirstOrDefault();
  37. InitializeComponent();
  38. init();
  39. }
  40. private void init()
  41. {
  42. List<ServicePhoto> list = baseConnect.thisBase.ServicePhoto.Where(tb => tb.ServiceID == thisService.ID).ToList();//получаем все дополнительные фото
  43. if (list != null)
  44. {
  45. if (list.Count > 0) TBNoPhoto.Visibility = Visibility.Collapsed;
  46. else TBNoPhoto.Visibility = Visibility.Visible;
  47. LVImageService.ItemsSource = list;
  48. }
  49. }
  50. private void BtnBackClick(object sender, RoutedEventArgs e)
  51. {
  52. dataFrame.mainFrame.Navigate(new pageEditService(thisService.ID));//возвращаемся на страницу редактирования услуги
  53. }
  54. private void BtnAddClick(object sender, RoutedEventArgs e)
  55. {
  56. try
  57. {
  58. OpenFileDialog OFD = new OpenFileDialog();
  59. OFD.Multiselect = true;
  60. if (OFD.ShowDialog() == true)
  61. {
  62. foreach (string file in OFD.FileNames)
  63. {
  64. ServicePhoto photo = new ServicePhoto();
  65. photo.ServiceID = thisService.ID;
  66. photo.PhotoPath = File.ReadAllBytes(file);
  67. baseConnect.thisBase.ServicePhoto.Add(photo);
  68. }
  69. }
  70. baseConnect.thisBase.SaveChanges();
  71. init();
  72. MessageBox.Show("Фото успешно загружено", "Успешно", MessageBoxButton.OK, MessageBoxImage.Information);
  73. }
  74. catch
  75. {
  76. MessageBox.Show("Не удалось загрузить фото", "Ошибка загрузки фото", MessageBoxButton.OK, MessageBoxImage.Error);
  77. }
  78. }
  79. private void BtnDeleteClick(object sender, RoutedEventArgs e)
  80. {
  81. Button btn = (Button)sender;
  82. int id = Convert.ToInt32(btn.Uid);
  83. ServicePhoto photo = baseConnect.thisBase.ServicePhoto.Where(tb => tb.ID == id).FirstOrDefault();
  84. if (photo != null)
  85. {
  86. try
  87. {
  88. baseConnect.thisBase.ServicePhoto.Remove(photo);
  89. baseConnect.thisBase.SaveChanges();
  90. init();
  91. }
  92. catch
  93. {
  94. MessageBox.Show("Ошибка удаления фото");
  95. }
  96. }
  97. }
  98. }
  99. }