private void Button_Click(object sender, RoutedEventArgs e) { try// на случай пустой или битой строки { if (dataGrid2.SelectedItem != null) { var selectedBook = (Book)dataGrid2.SelectedItem; var tempBook = new BookOrganizer.Book() { Title = selectedBook.Name, Annotation = selectedBook.Annotation, Author = new Author() { Name = selectedBook.Author }, Year = int.Parse(selectedBook.Year), Pages = int.Parse(selectedBook.Pages) }; var v = new AddBookView(); v.DataContext = new AddBookViewModel(tempBook); ((AddBookViewModel)v.DataContext).BookOut += (b) => { using (var c = new Context()) { c.Books.Add(b); c.SaveChanges(); } v.Close(); }; v.Show(); } } catch { } }
private void Button_Click(object sender, RoutedEventArgs e) { try// на случай пустой или битой строки { if (dataGrid2.SelectedItem != null) { var selectedBook = (Book)dataGrid2.SelectedItem; var tempBook = new BookOrganizer.Book() { Title = selectedBook.Name, Annotation = selectedBook.Annotation, Author = new Author() { Name = selectedBook.Author }, Year = int.Parse(selectedBook.Year), Pages = int.Parse(selectedBook.Pages) }; var v = new AddBookView(); v.DataContext = new AddBookViewModel(tempBook); ((AddBookViewModel)v.DataContext).BookOut += (b) => { using (var c = new Context()) { if (b.Author != null) { var k = c.Authors.FirstOrDefault(p => p.Name == b.Author.Name); if (k != null) { b.Author = k; } } if (b.Genre != null) { var t = c.Genres.FirstOrDefault(p => p.Name == b.Genre.Name); if (t != null) { b.Genre = t; } } c.Books.Add(b); c.SaveChanges(); } v.Close(); }; v.Show(); } } catch { } }
public void TestMethod1() { var book = new BookOrganizer.Book() { Title = "что-то", Author = new BookOrganizer.Author() { Name = "Пушкин" } }; var b = new AddBookViewModel(book); PrivateObject obj = new PrivateObject(b); Assert.AreEqual(book, obj.GetFieldOrProperty("book") as BookOrganizer.Book); }