using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace NET { /// /// Логика взаимодействия для MainWindow.xaml /// public partial class MainWindow : Window { private BookStoreContext _context; public MainWindow() { InitializeComponent(); _context = new BookStoreContext(); Load(); } private void Load() { BooksGrid.ItemsSource = _context.Books.ToList(); } private void BtnInsert_Click(object sender, RoutedEventArgs e) { Insert(TbName.Text, TbPrice.Text, TbAuthor.Text, TbCategory.Text); } public bool Insert(string name, string priceStr, string author, string category) { if (!decimal.TryParse(priceStr, out decimal price)) { return false; } Books book = new Books() { Name = name, Price = price, Author = author, Category = category }; _context.Books.Add(book); _context.SaveChanges(); Load(); return true; } private void BtnUpdate_Click(object sender, RoutedEventArgs e) { Update(TbName.Text, TbPrice.Text, TbAuthor.Text, TbCategory.Text); } public bool Update(string name, string priceStr, string author, string category) { if (BooksGrid.SelectedItem is Books selectedBook) { if (!decimal.TryParse(priceStr, out decimal price)) { return false; } selectedBook.Name = name; selectedBook.Price = price; selectedBook.Category = category; selectedBook.Author = author; _context.SaveChanges(); Load(); return true; } return false; } private void BtnDelete_Click(object sender, RoutedEventArgs e) { DeleteBook(); } public void DeleteBook() { if (BooksGrid.SelectedItem is Books selectedBook) { _context.Books.Remove(selectedBook); _context.SaveChanges(); Load(); } } private void BooksGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (BooksGrid.SelectedItem is Books selectedBook) { TbName.Text = selectedBook.Name; TbPrice.Text = selectedBook.Price.ToString(); TbAuthor.Text = selectedBook.Author; TbCategory.Text = selectedBook.Category; } } } }