如何在每次按钮单击事件后添加新项目并将以前的项目保留在通用列表中?



当用户单击按钮时,我想将新项目添加到我的通用列表中,但是当我单击该按钮时,我看到列表仅包含上次引入的项目。 似乎在每个按钮单击列表中都会重新初始化。 如何保留旧项目并将新项目添加到我的通用列表并在列表框中显示所有这些项目?

谢谢。。

C# 代码

namespace Example
{
/// <summary>
/// Interaction logic for CreateProduct.xaml
/// </summary>
public partial class CreateProduct : Window
{
public static float weight;
public static int quantity;
public static string customer, piece, material;
public CreateProduct()
{
InitializeComponent();
}
public static List<Liste> AddList()
{
List<Liste> list = new List<Liste>();
Liste kayit= new Liste();
kayit.Customer = customer;
kayit.Piece = piece;
kayit.Material = material;
kayit.Quantity = quantity;
kayit.Weight = weight;
list.Add(kayit);
return list;        
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
customer = btnEditCustomer1.Text;
piece = btnPiece.Text;
material = txtMaterial.Text;
quantity = Convert.ToInt32(txtQuantity.Text);
weight = float.Parse(txtWeight.Text);
if (customer != null && piece != null && material != null)
{
listBoxProduct.ItemsSource = AddList();
}
}
}
public class Liste
{
public string Customer { get; set; }
public string Piece { get; set; }
public string Material { get; set; }
public int Quantity { get; set; }
public float Weight { get; set; }
}    
}

XAML 代码

<ListBox Grid.Row="1" x:Name="listBoxProduct"  SelectionMode="Single"   Margin="0" Background="Transparent" ScrollViewer.VerticalScrollBarVisibility="Auto"  ScrollViewer.HorizontalScrollBarVisibility="Hidden" Height="200">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" Margin="0" Height="30" CornerRadius="4" Width="875"  Background="#2E323B" BorderBrush="Black">
<DockPanel>
<TextBlock Text="{Binding Customer}"  Foreground="White" TextWrapping="Wrap"  VerticalAlignment="Stretch" FontSize="16"  HorizontalAlignment="Left" Margin="4,0,0,0"/>
<TextBlock Text="{Binding Piece}"    Foreground="White"  TextWrapping="Wrap" VerticalAlignment="Stretch" FontSize="16"  HorizontalAlignment="Left" Margin="4,0,0,0"/>
<TextBlock Text="{Binding Material}"  Foreground="White"  TextWrapping="Wrap" VerticalAlignment="Stretch" FontSize="16"  HorizontalAlignment="Left" Margin="4,0,0,0"/>
<TextBlock Text="{Binding Quantity}"  Foreground="White"  TextWrapping="Wrap" VerticalAlignment="Stretch" FontSize="16"  HorizontalAlignment="Left" Margin="4,0,0,0"/>
<TextBlock Text="{Binding Weight}"  Foreground="White"  TextWrapping="Wrap" VerticalAlignment="Stretch" FontSize="16"  HorizontalAlignment="Left" Margin="4,0,0,0"/>
</DockPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

修复了代码的一些问题:

  • 尽可能避免static
  • 不要在每次点击时都创建一个新的列表实例,你会丢失以前的项目。 在窗口中声明一个字段。
  • listBox 需要知道何时添加新项才能显示它们。 但列表不会报告添加/删除。 使用ObservableCollection
public partial class CreateProduct : Window
{
private ObservableCollection<Liste> list = new ObservableCollection<Liste>();    
public CreateProduct()
{
InitializeComponent();
listBoxProduct.ItemsSource = list;
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
float weight;
int quantity;
string customer, piece, material;
customer = btnEditCustomer1.Text;
piece = btnPiece.Text;
material = txtMaterial.Text;
quantity = Convert.ToInt32(txtQuantity.Text);
weight = float.Parse(txtWeight.Text);
if (customer != null && piece != null && material != null)
{
Liste kayit = new Liste();
kayit.Customer = customer;
kayit.Piece = piece;
kayit.Material = material;
kayit.Quantity = quantity;
kayit.Weight = weight;
list.Add(kayit);
}
}
}

问题是您的AddList()每次单击按钮时都会创建一个新列表。您必须创建一个新属性,例如:

public  ObservableCollection<Liste> AllItems { get; set; } = new ObservableCollection<Liste>();

之后改变你的AddList()

public static Liste CreateItem()
{
Liste kayit= new Liste();
kayit.Customer = customer;
kayit.Piece = piece;
kayit.Material = material;
kayit.Quantity = quantity;
kayit.Weight = weight;
return kayit;
}

和您的btnAdd_Click()

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
customer = btnEditCustomer1.Text;
piece = btnPiece.Text;
material = txtMaterial.Text;
quantity = Convert.ToInt32(txtQuantity.Text);
weight = float.Parse(txtWeight.Text);
if (customer != null && piece != null && material != null)
{
AllItems.Add( CreateItem() );
}
}

因此,现在CreateItem()您的旧AddList()将创建一个新项,并且该项将在btnAdd_Click方法中添加到您的集合中。

编辑:

我错过的是你必须在构造函数中设置ItemSource

public CreateProduct()
{
InitializeComponent();
listBoxProduct.ItemsSource = AllItems;
}

网站注释:

我会改变你的整个btnAdd_Click方法 对此:

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
string customer = btnEditCustomer1.Text;
string piece = btnPiece.Text;
string material = txtMaterial.Text;
int quantity = Convert.ToInt32(txtQuantity.Text);
float weight = float.Parse(txtWeight.Text);
if (customer != null && piece != null && material != null)
{
var item = new Liste 
{
Customer = customer,
Piece = piece,
Material = material,
Quantity = quantity,
Weight = weight
};
AllItems.Add(item);
}
}

似乎你的代码完全搞砸了。 无论如何,让我给出一个适合您的快速解决方案。

static List<Liste> list = new List<Liste>();
public static List<Liste> ListeEkle()
{
Liste kayit= new Liste();
kayit.Musteri = musteri;
kayit.Parca = parca;
kayit.Malzeme = malzeme;
kayit.Adet = Adet;
kayit.Agirlik = Agirlik;
list.Add(kayit);
return list;

}

希望有帮助。

每次单击按钮时都会重新创建包含单个元素的列表,因为 AddList 方法会重新初始化列表(它每次都会创建一个新列表(。需要在 AddList 方法外部(以及按钮单击事件处理程序外部(创建列表。在AddList中,您应该创建一个项目(Liste(并将其添加到现有列表中,正如@Gopichandar的答案所指出的那样。

您可能还需要考虑使用列表到列表框的(单向(绑定,而不是在每次单击时重新创建列表框的 ItemSource。

最新更新