MainWindow.xaml.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Linq;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. namespace BookStore_
  5. {
  6. public partial class MainWindow : Window
  7. {
  8. private BookStoreContext _context;
  9. public MainWindow()
  10. {
  11. InitializeComponent();
  12. _context = new BookStoreContext();
  13. Load();
  14. }
  15. private void Load()
  16. {
  17. BookGrid.ItemsSource = _context.Books.ToList();
  18. }
  19. private void BtnUpdate_Click(object sender, RoutedEventArgs e)
  20. {
  21. if (BookGrid.SelectedItem is Book selectedBook)
  22. {
  23. if (!decimal.TryParse(TbPrice.Text, out decimal price))
  24. {
  25. return;
  26. }
  27. selectedBook.Name = TbName.Text;
  28. selectedBook.Price = price;
  29. selectedBook.Author = TbAuthor.Text;
  30. selectedBook.Category = TbCategory.Text;
  31. _context.SaveChanges();
  32. Load();
  33. }
  34. }
  35. private void BtnIsert_Click(object sender, RoutedEventArgs e)
  36. {
  37. if (!decimal.TryParse(TbPrice.Text, out decimal price))
  38. {
  39. return;
  40. }
  41. Book book = new()
  42. {
  43. Name = TbName.Text,
  44. Price = price,
  45. Author = TbAuthor.Text,
  46. Category = TbCategory.Text,
  47. };
  48. _context.Books.Add(book);
  49. _context.SaveChanges();
  50. Load();
  51. }
  52. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  53. {
  54. if (BookGrid.SelectedItem is Book selectedbook)
  55. {
  56. _context.Books.Remove(selectedbook);
  57. _context.SaveChanges();
  58. Load();
  59. }
  60. }
  61. private void BookGrid_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
  62. {
  63. if (BookGrid.SelectedItem is Book selectedBook)
  64. {
  65. TbName.Text = selectedBook.Name;
  66. TbPrice.Text = selectedBook.Price.ToString();
  67. TbAuthor.Text = selectedBook.Author;
  68. TbCategory.Text = selectedBook.Category;
  69. }
  70. }
  71. }
  72. }