C# 如何在每次按钮单击事件后在常规列表中添加新项目和保留以前的项目?

C# 如何在每次按钮单击事件后在常规列表中添加新项目和保留以前的项目?,c#,wpf,data-binding,listbox,C#,Wpf,Data Binding,Listbox,当用户单击某个按钮时,我想将新项目添加到我的通用列表中,但当我单击该按钮时,我发现列表中只包含最后引入的项目。 似乎在每个按钮单击期间,列表都会重新初始化。 如何保留旧项目并将新项目添加到常规列表中,并在列表框中显示所有项目 谢谢 C#代码 namespace Example { /// <summary> /// Interaction logic for CreateProduct.xaml /// </summary> public part

当用户单击某个按钮时,我想将新项目添加到我的通用列表中,但当我单击该按钮时,我发现列表中只包含最后引入的项目。 似乎在每个按钮单击期间,列表都会重新初始化。 如何保留旧项目并将新项目添加到常规列表中,并在列表框中显示所有项目

谢谢

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; }
    }    
}
<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>
名称空间示例
{
/// 
///CreateProduct.xaml的交互逻辑
/// 
公共部分类CreateProduct:Window
{
公共静态浮力;
公共静态整数;
公共静串客户、件、料;
公共产品()
{
初始化组件();
}
公共静态列表AddList()
{
列表=新列表();
Liste kayit=new Liste();
kayit.Customer=Customer;
kayit.Piece=件;
kayit.Material=材料;
kayit.数量=数量;
kayit.重量=重量;
列表。添加(kayit);
退货清单;
}
私有无效btnAdd_单击(对象发送者,路由目标e)
{
customer=btnEditCustomer1.Text;
工件=btnPiece.Text;
material=txtmatterial.Text;
数量=转换为32(txtQuantity.Text);
weight=float.Parse(txtwweight.Text);
如果(客户!=null&件!=null&材料!=null)
{
listBoxProduct.ItemsSource=AddList();
}
}
}
公共类列表
{
公共字符串Customer{get;set;}
公共字符串{get;set;}
公共字符串材质{get;set;}
公共整数数量{get;set;}
公共浮动权重{get;set;}
}    
}
XAML代码

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


}
静态列表=新列表();
公共静态列表listekle()
{
Liste kayit=new Liste();
kayit.Musteri=Musteri;
kayit.Parca=Parca;
kayit.Malzeme=Malzeme;
kayit.Adet=Adet;
kayit.Agirlik=Agirlik;
列表。添加(kayit);
退货清单;
}

希望有帮助

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


您可能还想考虑使用列表(单行)绑定到列表框,而不是在每次单击时重新创建ListBox的ItMeStase.< /P> < P>问题是,您的<代码> AddiList](/Cult>)在每次单击按钮时创建一个新的列表。您必须创建一个新属性,例如:

public  ObservableCollection<Liste> AllItems { get; set; } = new ObservableCollection<Liste>();
然后您的
b添加单击()

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\u Click
方法中的集合中

编辑:

我没有说的是,您必须在构造函数中设置
ItemSource

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

网站注:

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


修复代码中的一些问题:

  • 尽可能避免
    静态
  • 不要在每次单击时创建列表的新实例,否则会丢失以前的项目。在窗口中声明一个字段
  • listBox需要知道何时添加新项目以显示它们。但该列表不报告添加/删除。使用
    observedcollection
公共部分类CreateProduct:Window
{
私有ObservableCollection列表=新ObservableCollection();
公共产品()
{
初始化组件();
listBoxProduct.ItemsSource=列表;
}
私有无效btnAdd_单击(对象发送者,路由目标e)
{
浮重;
整数;
串客户、件、料;
customer=btnEditCustomer1.Text;
工件=btnPiece.Text;
material=txtmatterial.Text;
数量=转换为32(txtQuantity.Text);
weight=float.Parse(txtwweight.Text);
如果(客户!=null&件!=null&材料!=null)
{
Liste kayit=new Liste();
kayit.Customer=Customer;
kayit.Piece=件;
kayit.Material=材料;
kayit.数量=数量;
kayit.重量=重量;
列表。添加(kayit);
}
}
}

谢谢你,我的朋友。但是它不起作用。它只起作用一次。但是当我单击按钮时,其他什么都没有发生。@Baris你必须删除
列表
从内部创建的
listekle
方法,并将其保留在前面提到的方法之外。尝试使用英语作为你的类和属性名,这是有点难以理解,这是你似乎新的WPF(这是好的)。但是你很难理解你得到的所有建议。所以,我认为你可以通过“聊天模式”一步一步地学习。关于wpf(不仅仅是wpf)的相关讨论真是太棒了。谢谢。我试过了,但我做不到。你能写更多的解释吗?非常感谢Martin。
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);
       }
    }
}