Je souhaite ajouter de nouveaux éléments à ma liste générique lorsque l'utilisateur clique sur un bouton, mais lorsque je clique sur le bouton, je vois que la liste ne contient que le dernier élément introduit. Il semble que lors de chaque bouton, la liste de clic soit réinitialisée. Comment puis-je conserver les anciens éléments et ajouter de nouveaux éléments à ma liste générique et les afficher tous dans la liste?Comment ajouter de nouveaux éléments et les conserver dans une liste générique après chaque clic sur un événement?
Merci ..
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>
essayez d'utiliser l'anglais comme classe et les noms de propriété, il est un peu difficile à comprendre qui est qui – Kokombads
Il semble que vous nouveau pour WPF (il est normal). Mais il peut être difficile de comprendre tous les conseils que vous obtenez. Donc, je pense que vous pouvez mieux apprendre avec "mode conversation" étape par étape. Il y a une salle de discussion impressionnante (https://chat.stackoverflow.com/rooms/18165/wpf) pour les discussions relatives à wpf (pas seulement). –