Qwerty.xaml.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.Shapes;
  14. namespace OUP
  15. {
  16. public partial class Qwerty : Window
  17. {
  18. private Context _context;
  19. public Qwerty()
  20. {
  21. InitializeComponent();
  22. _context = new Context();
  23. Load();
  24. }
  25. private void Load()
  26. {
  27. ProductsGrid.ItemsSource = _context.products.ToList();
  28. }
  29. private void BtnInsert_Click(object sender, RoutedEventArgs e)
  30. {
  31. if (!decimal.TryParse(TbPrice.Text, out decimal price))
  32. {
  33. return;
  34. }
  35. Product2 product = new Product2()
  36. {
  37. Name = TbProduct.Text,
  38. Price = price,
  39. Supplier = TbSupplier.Text,
  40. Dates = TbDate.Text,
  41. };
  42. // _context.products.Add(product);
  43. _context.SaveChanges();
  44. Load();
  45. }
  46. private void BtnUpdate_Click(object sender, RoutedEventArgs e)
  47. {
  48. if (ProductsGrid.SelectedItem is Product2 selectedProduct2)
  49. {
  50. if (!decimal.TryParse(TbPrice.Text, out decimal price))
  51. {
  52. return;
  53. }
  54. selectedProduct2.Name = TbProduct.Text;
  55. selectedProduct2.Price = price;
  56. selectedProduct2.Supplier = TbSupplier.Text;
  57. selectedProduct2.Dates = TbDate.Text;
  58. _context.SaveChanges();
  59. Load();
  60. }
  61. }
  62. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  63. {
  64. if (ProductsGrid.SelectedItem is Product2 selectedproducts)
  65. {
  66. //_context.products.Remove(selectedproducts);
  67. _context.SaveChanges();
  68. Load();
  69. }
  70. }
  71. private void BooksGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  72. {
  73. if (ProductsGrid.SelectedItem is Product2 selectedproducts)
  74. {
  75. TbProduct.Text = selectedproducts.Name;
  76. TbPrice.Text = selectedproducts.Price.ToString();
  77. TbSupplier.Text = selectedproducts.Supplier;
  78. TbDate.Text = selectedproducts.Dates;
  79. }
  80. }
  81. }
  82. }