HotelsPage.xaml.cs 2.5 KB

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