示例#1
0
        /// <summary>
        /// Removing author from the static list on button click
        /// </summary>
        /// <param name="sender">The source of the event</param>
        /// <param name="e">The instance containing the event data</param>
        private void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            if (Authors.AuthorsList.Count < 1)
            {
                MessageBox.Show("No authors to remove.");
                return;
            }

            AuthorDetailViewModel viewmodel = (AuthorDetailViewModel)DataContext;
            Author author = Authors.GetAuthor(viewmodel.MyAuthor.AuthorId);

            if (author.BooksList.Count > 0)
            {
                MessageBox.Show("Author cannot be removed. Associated books must be removed first.");
            }
            else if (MessageBox.Show("Are you sure you want to remove the author?", "Remove author", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                Authors.RemoveAuthor(author.AuthorId);
                Authors.AreRemovedItems = true;
                var mw = Application.Current.Windows
                         .Cast <Window>()
                         .FirstOrDefault(window => window is MainWindow) as MainWindow;

                mw.DataContext = new
                {
                    collection = new AuthorsListViewModel(),
                    detail     = new AuthorDetailViewModel(Authors.GetAuthor(Authors.AuthorsList.Count))
                };
            }
        }
示例#2
0
        /// <summary>
        /// Loading right context to the main window
        /// </summary>
        private void ActivateMainWindow()
        {
            var mw = Application.Current.Windows
                     .Cast <Window>()
                     .FirstOrDefault(window => window is MainWindow) as MainWindow;

            switch (mw.comboBox.SelectedIndex)
            {
            case 0:
                mw.DataContext = new
                {
                    collection = new BooksListViewModel(),
                    detail     = new BookDetailViewModel(Books.GetBook(1))
                };
                break;

            case 1:
                mw.DataContext = new
                {
                    collection = new AuthorsListViewModel(),
                    detail     = new AuthorDetailViewModel(Authors.GetAuthor(1))
                };
                break;

            case 2:
                mw.DataContext = new
                {
                    collection = new GenresListViewModel(),
                    detail     = new GenreDetailViewModel(Genres.GetGenre(1))
                };
                break;

            default:
                break;
            }
        }