コード例 #1
0
        private void removeFromCart(object sender, RoutedEventArgs e)
        {
            Button        button = sender as Button;
            ElementOfCart p      = button.DataContext as ElementOfCart;

            cart.totalPrice        -= p.product.Price * p.quantity;
            textBlock.DataContext   = cart.totalPrice;
            cartListBox.ItemsSource = null;
            cartListBox.ItemsSource = cart;
            cart.Remove(p);
        }
コード例 #2
0
        private void plus(object sender, RoutedEventArgs e)
        {
            Button        button = sender as Button;
            ElementOfCart p      = button.DataContext as ElementOfCart;

            p.quantity++;
            cart.totalPrice        += p.product.Price;
            textBlock.DataContext   = cart.totalPrice;
            cartListBox.ItemsSource = null;
            cartListBox.ItemsSource = cart;
        }
コード例 #3
0
        private void addToCart(object sender, RoutedEventArgs e)
        {
            var     a      = sender as Product;
            Button  button = sender as Button;
            Product p      = button.DataContext as Product;
            Window1 w      = new Window1(p.Category);

            w.ShowInTaskbar = false;
            w.Owner         = Application.Current.MainWindow;
            w.ShowDialog();
            if (w.DialogResult == true)
            {
                int q;
                try
                {
                    q = Convert.ToInt32(w.textBlock.Text);
                    cart.totalPrice      += q * p.Price;
                    textBlock.DataContext = cart.totalPrice;
                    foreach (var item in cart)
                    {
                        if (item.product == p)
                        {
                            var el = new ElementOfCart(p, item.quantity + q);
                            cart.Remove(item);
                            cart.Add(el);
                            return;
                        }
                    }
                    cart.Add(new ElementOfCart(p, q));
                }
                catch
                {
                    MessageBox.Show("Invalid format of price", null, MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }
            else
            {
                w.Close();
            }
        }