简体   繁体   English

每当我单击 C# 中的按钮时,如何将 Append 项添加到另一个 Window 中的列表框?

[英]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.

相关问题 如何单击一个按钮,该按钮将从ListBox中删除一个项目,而该列表中包含C#中ListBox的信息? - How do I click a button that will remove an item from a ListBox and the List that holds the information for the ListBox in C#? 如何通过单击按钮C#将列表框项目传输到文本框中 - How Do I Transfer Listbox Items Into A Textbox By Clicking A Button C# C#如何刷新列表框中的项目 - c# how do i refresh items in my listbox 当我使用 C# 单击 WPF 窗口上的按钮时,如何将文本发送到活动窗口? - How do I send text to an active window when I click a button on a WPF window using C#? C#-如何在另一个列表框中打印字符串? - C# - How do I print a string in another listbox? 如何在不违反MVVM的情况下将项目追加到ListBox - How do I append items to a ListBox without violating MVVM 如何通过单击一个按钮选择C#复选框控件中的前30个项目 - How do I select the top 30 items in a C# checkbox control with a single button click 如何在单击按钮时使用另一个列表框在列表框中插入项目 - How to insert items inside listbox withing another listbox on button click 如何从另一个处理程序调用/调用按钮单击事件处理程序? (C# ) - How Do I invoke/call a button click event handler form another handler? (c# ) 使用C#,如何更新另一个类的列表框项? - Using C#, how can I update listbox Items from another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM