如何在每个按钮单击事件之后添加新的并保留通用列表中的前一项?

问题描述:

我想添加新的项目到我的通用列表当用户点击一个按钮,但是当我点击按钮,我sehathat列表只包含最后介绍的项目。 似乎在每个按钮单击列表中获得重新初始化。 如何保留旧项目并将新项目添加到我的通用列表中并将它们全部显示在列表框中?如何在每个按钮单击事件之后添加新的并保留通用列表中的前一项?

谢谢。

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

尝试使用英语作为你的类和属性名,这是有点难以理解的是,似乎这 – Kokombads

+0

像你新的WPF(没关系)。但是很难理解你得到的所有建议。所以,我认为你可以更好地用“聊天模式”一步一步学习。关于wpf(不仅仅是)讨论,有很棒的[聊天室](https://chat.*.com/rooms/18165/wpf)。 –

解决的几个问题与您的代码:

  • 避免static尽可能
  • 不要在每次点击时创建一个List的新实例,您将丢失以前的项目。在窗口中声明一个字段。
  • listBox需要知道什么时候添加新项目来显示它们。但List不报告添加/删除。使用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); 
     } 
    } 
} 
+0

非常感谢你@ ASh.It工作出色。我发现ObservableCollection感谢你。 – Barsblue

好像你的代码是完全搞砸了。无论如何,让我给你一个快速解决方案。

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; 


} 

希望有所帮助。

+0

谢谢你,我的朋友。但它不工作。它只工作一次。但是当我点击按钮时,没有其他的事情发生。 – Barsblue

+0

@Baris你必须从'ListeEkle'方法内删除'list'创建方法,并将其保留在上述方法之外。 – Gopichandar

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

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

+0

谢谢。我试过了,但我不能管理。你能写得更具说明性吗? – Barsblue

问题是,您的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; 
} 

SiteNote:

我想你的整个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); 
    } 
} 
+0

非常感谢Martin。 – Barsblue