[英]adding items to a listbox from another wpf window
so I have my MainWindow that contains a list box menu_listbox
and a button Add_menu_item
that when clicked will open a second wpf window New_menu_item
in which I have texboxes and a button Add_new_menu_item
when the butoon is clicked it will close the New_menu_item
window and add the values to the class new_listbox_item
when I try and add a new item all the previous items are updated to new item added. 所以我有我的主窗口,其中包含一个列表框
menu_listbox
和一个按钮Add_menu_item
,当点击后打开第二个WPF窗口New_menu_item
中,我有texboxes和一个按钮Add_new_menu_item
当butoon被点击它将关闭New_menu_item
窗口和值添加到当我尝试添加新项目时,类为new_listbox_item
,所有以前的项目都更新为添加的新项目。 the code use is bellow 代码使用如下
private void add_menu_item_Click(object sender, RoutedEventArgs e)
{
new_menu_item form1 = new new_menu_item();
form1.ShowDialog();
new_listbox_item M_item = new new_listbox_item();
M_item.new_item_name = form1.new_food.Text;
M_item.price = double.Parse(form1.food_price.Text);
M_item.vegitarian = form1.is_vegetarian.IsChecked == true;
menu_Listbox.Items.Add(M_item);
}
does anyone have any ideas on how to solve this issue? 有没有人对如何解决这个问题有任何想法? the
new_listbox_item
looks like this: new_listbox_item
如下所示:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MainServerWIndow
{
public class new_listbox_item
{
private static string _item_name;
private static Boolean _vegitarian;
private static double _price;
private static double _total;
private double N_price;
public string new_item_name
{
get
{
return _item_name;
}
set
{
_item_name = value;
}
}
public Boolean vegitarian
{
get
{
return _vegitarian;
}
set
{
_vegitarian = value;
}
}
public double price
{
get
{
return _price;
}
set
{
_price = value;
}
}
public static string item_name
{
get
{
return _item_name;
}
set
{
_item_name = value;
}
}
public static double item_price
{
get
{
return _price;
}
set
{
_price = value;
}
}
public static Boolean vegetarian
{
get
{
return _vegitarian;
}
set
{
_vegitarian = value;
}
}
public static double total
{
get
{
return _total;
}
set
{
_total = value;
}
}
public override string ToString()
{
return string.Format("{0} {1} {2}", new_item_name, vegitarian, price);
}
}
}
all the previous items are updated to new item added
所有先前的项目都更新为添加的新项目
Without a good, minimal , complete code example that reliably reproduces the problem it is impossible to know for sure what the problem is. 没有可靠,可靠地重现问题的良好, 最少 , 完整的代码示例 ,就无法确定问题的根源。 But given the code you posted, there is certainly one serious code defect that would explain your problem.
但是考虑到您发布的代码,肯定有一个严重的代码缺陷可以解释您的问题。 You have declared all but one of the fields in your class as
static
: 您已将类中除一个字段以外的所有字段声明为
static
:
public class new_listbox_item
{
private static string _item_name;
private static Boolean _vegitarian;
private static double _price;
private static double _total;
...
}
By doing so, you have caused every instance of the new_listbox_item
class to share the fields. 这样,您导致
new_listbox_item
类的每个实例共享字段。 Setting the field in one instance causes every other instance to use the new value; 在一个实例中设置字段会导致其他所有实例使用新值; each instance does not have its own local copy of the field.
每个实例都没有自己的字段本地副本。
Remove the static
keyword from all of those fields, and that will probably get your code working the way you want. 从所有这些字段中删除
static
关键字,这可能会使您的代码按您希望的方式工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.