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 BookStokeSQL { /// /// Логика взаимодействия для MainWindow.xaml /// public partial class MainWindow : Window { private BookStoreContext context_; public MainWindow() { InitializeComponent(); context_ = new BookStoreContext(); Load(); } private void Load() { if (context_.Books.ToList().Count != 0) BooksGrid.ItemsSource = context_.Books.ToList(); } private void BtnInsert_CLick(object sender, RoutedEventArgs e) { if (!decimal.TryParse(TbPrice.Text, out decimal price)) { return; } Book book = new Book() { Name = TbName.Text, Price = price, Author = TbAuthor.Text, Category = TbCategory.Text, }; context_.Books.Add(book); context_.SaveChanges(); Load(); } private void BtnUpdate_Click(object sender, RoutedEventArgs e) { if (BooksGrid.SelectedItem is Book selectedBook) { if (!decimal.TryParse(TbPrice.Text, out decimal price)) { return; } selectedBook.Name = TbName.Text; selectedBook.Price = price; selectedBook.Author = TbAuthor.Text; selectedBook.Category = TbCategory.Text; context_.SaveChanges(); Load(); } } private void BtnDelete_Click(object sender, RoutedEventArgs e) { if (BooksGrid.SelectedItem is Book selectedBook) { context_.Books.Remove(selectedBook); context_.SaveChanges(); Load(); } } private void BooksGrid_SelectionChanged(object sender, RoutedEventArgs e) { if (BooksGrid.SelectedItem is Book selectedBook) { TbName.Text = selectedBook.Name; TbPrice.Text = selectedBook.Price.ToString(); TbAuthor.Text = selectedBook.Author; TbCategory.Text = selectedBook.Category; } } } }