Baris Baris - 5 months ago 23
C# Question

How to add new and keep previous items in a generic List after each button click event?

I want to add new items to my generic list when user clicks on a button, but when i click the button,i seethat the the list contains only the last introduced item.
It seems that during each button click list get reinitialized.
How can i keep old items and add new items to my generic list and display all of them in the listbox?

Thank you..

C# Code

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 Code

<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>

ASh ASh
Answer Source

fix a few issue with your code:

  • avoid static when possible
  • don't create a new instance of List on every click, you are loosing previous items. declare a field in a Window.
  • listBox needs to know when new items are added to display them. but List doesn't report about additions/removals. use 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);
       }
    }
}