1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- namespace bookstore
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- 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)
- {
- if(!decimal.TryParse(TbPrice.Text, out decimal price))
- {
- return;
- }
- book book = new()
- {
- Name = TbName.Text,
- Price = price,
- Author = TbAuthor.Text,
- Category = TbCategoty.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 = TbCategoty.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, SelectionChangedEventArgs e)
- {
- if(BooksGrid.SelectedItem is book selectedBook)
- {
- TbName.Text = selectedBook.Name;
- TbPrice.Text = selectedBook.Price.ToString();
- TbAuthor.Text = selectedBook.Author;
- TbCategoty.Text = selectedBook.Category;
- }
- }
-
- }
- }
|