// Adds the item to the shoppingcart private void AddFanToCart(object sender, EventArgs e) { Button button = (Button)sender; Fan fan = new Fan { Name = button.Tag.ToString(), Price = decimal.Parse(button.DataContext.ToString()), }; itemsInCartListBox.Items.Add($"{fan.Name}: {fan.Price}kr"); itemsInCart.Add(fan); UpdateTotalPrices(); }
// Loads the shopping cart if its saved private void LoadShoppingCart() { if (File.Exists(tempPath)) { foreach (var item in File.ReadAllLines(tempPath).Select(a => a.Split(","))) { Fan fan = new Fan { Name = item[0], Price = decimal.Parse(item[1]) }; itemsInCartListBox.Items.Add($"{fan.Name}: {fan.Price}kr"); itemsInCart.Add(fan); } UpdateTotalPrices(); } }
// Displays all the products from Products.csv private WrapPanel ProductList() { Fan fan = new Fan(); WrapPanel wrappanel = new WrapPanel { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Width = 200, Margin = new Thickness(20) }; // loops through the products.csv file and displays all the items in it try { foreach (var item in File.ReadAllLines(@"products.csv").Select(a => a.Split(","))) { fan.Name = item[0]; fan.Description = item[1]; fan.Price = decimal.Parse(item[2]); fan.ImageName = item[3]; TextBlock productName = new TextBlock { Text = fan.Name, FontFamily = new FontFamily("Comic Sans MS") }; wrappanel.Children.Add(productName); ImageSource imgSource = new BitmapImage(new Uri(@"Images\" + fan.ImageName, UriKind.Relative)); Image image = new Image { Source = imgSource, Width = 100, Height = 100, }; wrappanel.Children.Add(image); TextBlock productDescription = new TextBlock { Text = fan.Description, FontFamily = new FontFamily("Comic Sans MS"), Margin = new Thickness(5), }; wrappanel.Children.Add(productDescription); var buyButton = new Button { Content = "Buy", Tag = fan.Name, DataContext = fan.Price, FontFamily = new FontFamily("Comic Sans MS"), Width = 40, }; wrappanel.Children.Add(buyButton); buyButton.Click += AddFanToCart; } } // If there are no products or it cant find the csv file it says "Sorry there are no products" catch (Exception) { TextBlock noProducts = new TextBlock { Text = "Sorry there are no products", FontFamily = new FontFamily("Comic Sans MS"), FontSize = 15 }; wrappanel.Children.Add(noProducts); } return(wrappanel); }