HotelsPage.xaml.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace LR1_05._04
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для HotelsPage.xaml
  19. /// </summary>
  20. public partial class HotelsPage : Page
  21. {
  22. public HotelsPage()
  23. {
  24. InitializeComponent();
  25. DGridHotels.ItemsSource = ToursEntities.GetContext().Hotels.ToList();
  26. }
  27. private void Button_Click (object sender, RoutedEventArgs e)
  28. {
  29. }
  30. private void BtnEdit_Click(object sender, RoutedEventArgs e)
  31. {
  32. Manager.MainFrame.Navigate(new AddEditPage((sender as Button).DataContext as Hotel));
  33. }
  34. private void BtnAdd_Click(object sender, RoutedEventArgs e)
  35. {
  36. Manager.MainFrame.Navigate(new AddEditPage(null));
  37. }
  38. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  39. {
  40. var hotelsForRemoving = DGridHotels.SelectedItems.Cast<Hotel>().ToList();
  41. if (MessageBox.Show($"Вы точно хотите удалить следующие {hotelsForRemoving.Count()} элементов?", "Внимание",
  42. MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  43. {
  44. try
  45. {
  46. ToursEntities.GetContext().Hotels.RemoveRange(hotelsForRemoving);
  47. ToursEntities.GetContext().SaveChanges();
  48. MessageBox.Show("Данные удалены!");
  49. DGridHotels.ItemsSource = ToursEntities.GetContext().Hotels.ToList();
  50. }
  51. catch(Exception ex)
  52. {
  53. MessageBox.Show(ex.Message.ToString());
  54. }
  55. }
  56. }
  57. private void Page_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  58. {
  59. if(Visibility == Visibility.Visible)
  60. {
  61. ToursEntities.GetContext().ChangeTracker.Entries().ToList().ForEach(p => p.Reload());
  62. DGridHotels.ItemsSource = ToursEntities.GetContext().Hotels.ToList();
  63. }
  64. }
  65. }
  66. }