private void AddProductButton_OnClick(object sender, RoutedEventArgs e) { Button button = sender as Button; var dialog = new AddModifyProductDialog(null, _viewModel.Allergens.ToList(), _viewModel.ProductCategory.ToList()); dialog.Owner = this; dialog.ShowDialog(); if (dialog.DialogResult == true) { List <Allergen> listOfAllergens = new List <Allergen>(); var product = dialog.Product; foreach (var item in dialog.ModifyProducts.BAllergens) { if (item.IsChecked) { listOfAllergens.Add(item.Item); } } product.ProductCategory = dialog.ModifyProducts.SelectedPC; product.Allergens = listOfAllergens; _viewModel.ProductRepository.AddNewProduct(product); _viewModel.Products.Add(product); _viewModel.productsPageInfo.TotalItems = _viewModel.ProductRepository.ProductRepository.Count(); if (_viewModel.productsPageInfo.CurrentPage < _viewModel.productsPageInfo.TotalPages) { _viewModel.productsPageInfo.CurrentPage++; _viewModel.Products = new ObservableCollection <Product>(_viewModel.ProductRepository.ProductRepository.OrderBy(p => p.ProductId) .Skip((_viewModel.productsPageInfo.CurrentPage - 1) * _viewModel.productsPageInfo.ItemsPerPage) .Take(_viewModel.productsPageInfo.ItemsPerPage)); } DataContext = null; DataContext = _viewModel; } }
private void EditProductButton_OnClick(object sender, RoutedEventArgs e) { var selected = _viewModel.SelectedProduct; Button button = sender as Button; Product copy = new Product(button.DataContext as Product); var dialog = new AddModifyProductDialog(copy, _viewModel.Allergens.ToList(), _viewModel.ProductCategory.ToList()); dialog.Owner = this; dialog.ShowDialog(); if (dialog.DialogResult == true) { List <Allergen> listOfAllergens = new List <Allergen>(); foreach (var item in dialog.ModifyProducts.BAllergens) { if (item.IsChecked) { listOfAllergens.Add(new Allergen(item.Item)); } } copy.Allergens = listOfAllergens; copy.ProductCategory = dialog.ModifyProducts.SelectedPC; for (int i = 0; i < _viewModel.Products.Count; i++) { if (_viewModel.Products[i].ProductId == copy.ProductId) { _viewModel.ProductRepository.ChangeProduct(_viewModel.Products[i], copy); _viewModel.Products[i] = copy; } } } }