[英]How Do I Append Items to a Listbox in Another Window Whenever I Click a Button in C#?
I'm working on a WPF app on a Wawa touchscreen simulator.我正在 Wawa 触摸屏模拟器上开发 WPF 应用程序。 Whenever I click on a button in the menu (eg, a beverage), the item's name and price will be displayed on a Listbox in another window (eg, "Frozen Smoothie", 1.99).
每当我单击菜单中的一个按钮(例如饮料)时,该项目的名称和价格将显示在另一个 window 的列表框中(例如,“Frozen Smoothie”,1.99)。 The images below show what's happening.
下面的图片显示了正在发生的事情。
However, if I select another item (eg, a frozen cappuccino), that item's information replaces the previous item I selected.但是,如果我 select 另一个项目(例如,冷冻卡布奇诺),该项目的信息将替换我之前选择的项目。
I've made a Receipt class that takes the food item's name and price to create a Receipt object called foodItem, which is added to the Listbox.我制作了一个收据 class,它采用食品的名称和价格来创建一个名为 foodItem 的收据 object,并将其添加到列表框中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wawa_TouchScreen
{
public class Receipt
{
private string foodItemName;
private double price;
public Receipt(string foodItemName, double price)
{
this.foodItemName = foodItemName;
this.price = price;
}
public override string ToString()
{
string foodItem = foodItemName + " $" + price;
return foodItem.ToString();
}
public string getFoodItemName()
{
return foodItemName;
}
public double getPrice()
{
return price;
}
}
}
Here's the code for the beverage window:这是饮料 window 的代码:
public partial class Window1 : Window
{
public string foodItemName;
public double price;
private void frozenSmoothieBtn_Click(object sender, RoutedEventArgs e)
{
foodItemName = "Frozen Smoothie";
price = 1.99;
Receipt foodItem = new Receipt(foodItemName, price);
MessageBox.Show("You have chosen a frozen smoothie.", foodItemName);
Window2 checkOut = new Window2();
checkOut.Show();
this.Visibility = Visibility.Hidden;
checkOut.lstBoxReceipt.Items.Add(foodItem);
}
private void frozenCappuccinoBtn_Click(object sender, RoutedEventArgs e)
{
foodItemName = "Frozen Cappucino";
price = 1.99;
Receipt foodItem = new Receipt(foodItemName, price);
MessageBox.Show("You have chosen a frozen cappuccino.", foodItemName);
Window2 checkOut = new Window2();
checkOut.Show();
this.Visibility = Visibility.Hidden;
checkOut.lstBoxReceipt.Items.Add(foodItem);
}
}
}
I'd appreciate any help you can give.如果您能提供任何帮助,我将不胜感激。 Thanks!
谢谢!
To provide an immediate fix to your issue, you need to create a shared list (orderList) to bind to the lstBoxReceipt Listbox.要立即解决您的问题,您需要创建一个共享列表 (orderList) 以绑定到 lstBoxReceipt 列表框。 All operations of adding and removing the Receipt types should be performed on the shared list.
所有添加和删除 Receipt 类型的操作都应该在共享列表上执行。
We are using an ObservableCollection instead of List because if you use a List your changes to the underlying list object will not be immediately displayed on the Listbox.我们使用 ObservableCollection 而不是 List,因为如果您使用 List,您对基础列表 object 的更改将不会立即显示在 Listbox 上。 It will only display the first item.
它只会显示第一个项目。
Also, try improving your code by refactoring the button click events to call a method that adds the receipt to the list and another method that displays the checkout window. Instantiate the checkout window once by moving the window creation code outside the event code.此外,尝试通过重构按钮单击事件来改进您的代码,以调用将收据添加到列表的方法和另一个显示结帐 window 的方法。通过将 window 创建代码移到事件代码之外来实例化结帐 window 一次。
public partial class MainWindow : Window
{
public string foodItemName;
public double price;
//Add a shared list that you operate on adding and removing your food items from
//then assign it to the lstBoxReceipt.ItemsSource
ObservableCollection<Receipt> orderList = new ObservableCollection<Receipt>();
//Instantiate the checkOut window
Window2 checkOut = new Window2();
private void frozenSmoothieBtn_Click(object sender, RoutedEventArgs e)
{
foodItemName = "Frozen Smoothie";
price = 1.99;
Receipt foodItem = new Receipt(foodItemName, price);
MessageBox.Show("You have chosen a frozen smoothie.", foodItemName);
orderList.Add(foodItem);
checkOut.Show();
this.Visibility = Visibility.Hidden;
//Add the order list as the data source of the listbox.
checkOut.lstBoxReceipt.ItemsSource = orderList;
}
private void frozenCappuccinoBtn_Click(object sender, RoutedEventArgs e)
{
foodItemName = "Frozen Cappucino";
price = 1.99;
Receipt foodItem = new Receipt(foodItemName, price);
MessageBox.Show("You have chosen a frozen cappuccino.", foodItemName);
orderList.Add(foodItem);
checkOut.Show();
this.Visibility = Visibility.Hidden;
//Add the order list as the data source of the listbox.
checkOut.lstBoxReceipt.ItemsSource = orderList;
}
}
I'm working on a WPF app on a Wawa touchscreen simulator.我正在 Wawa 触摸屏模拟器上开发 WPF 应用程序。 Whenever I click on a button in the menu (eg, a beverage), the item's name and price will be displayed on a Listbox in another window (eg, "Frozen Smoothie", 1.99).
每当我单击菜单中的按钮(例如,饮料)时,项目的名称和价格将显示在另一个 window 的列表框中(例如,“Frozen Smoothie”,1.99)。 The images below show what's happening.
下面的图片显示了正在发生的事情。
However, if I select another item (eg, a frozen cappuccino), that item's information replaces the previous item I selected.但是,如果我 select 另一个项目(例如,冷冻卡布奇诺),该项目的信息将替换我之前选择的项目。
I've made a Receipt class that takes the food item's name and price to create a Receipt object called foodItem, which is added to the Listbox.我制作了一个收据 class,它采用食品的名称和价格来创建一个名为 foodItem 的收据 object,它被添加到列表框中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wawa_TouchScreen
{
public class Receipt
{
private string foodItemName;
private double price;
public Receipt(string foodItemName, double price)
{
this.foodItemName = foodItemName;
this.price = price;
}
public override string ToString()
{
string foodItem = foodItemName + " $" + price;
return foodItem.ToString();
}
public string getFoodItemName()
{
return foodItemName;
}
public double getPrice()
{
return price;
}
}
}
Here's the code for the beverage window:这是饮料 window 的代码:
public partial class Window1 : Window
{
public string foodItemName;
public double price;
private void frozenSmoothieBtn_Click(object sender, RoutedEventArgs e)
{
foodItemName = "Frozen Smoothie";
price = 1.99;
Receipt foodItem = new Receipt(foodItemName, price);
MessageBox.Show("You have chosen a frozen smoothie.", foodItemName);
Window2 checkOut = new Window2();
checkOut.Show();
this.Visibility = Visibility.Hidden;
checkOut.lstBoxReceipt.Items.Add(foodItem);
}
private void frozenCappuccinoBtn_Click(object sender, RoutedEventArgs e)
{
foodItemName = "Frozen Cappucino";
price = 1.99;
Receipt foodItem = new Receipt(foodItemName, price);
MessageBox.Show("You have chosen a frozen cappuccino.", foodItemName);
Window2 checkOut = new Window2();
checkOut.Show();
this.Visibility = Visibility.Hidden;
checkOut.lstBoxReceipt.Items.Add(foodItem);
}
}
}
I'd appreciate any help you can give.我很感激你能提供的任何帮助。 Thanks!
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.