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

蓝条

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

谢谢..

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尽可能避免
  • 不要在每次点击时创建一个新的 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);
       }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在列表中添加新项目?

来自分类Dev

动态刷新后如何将旧项目保留在列表视图中?

来自分类Dev

WPF ComboBox 项目在 Visibility.Collapsed 后保留在列表中

来自分类Dev

如何在将单元保留在项目中的同时从单元中删除表单?

来自分类Dev

如何在按钮单击事件上将项目添加到列表视图?

来自分类Dev

Android:将新项目添加到列表后,在ListView中单击NullPointerException onItemClick

来自分类Dev

将XElement添加到列表中,并将其子元素保留在列表中

来自分类Dev

每次单击按钮时,如何在列表框中添加项目?

来自分类Dev

Angular 2 - 添加新项目后如何反转 Firebase 列表

来自分类Dev

如何在Android Xamarin Visual Studio中单击按钮上的MvxListView上显示新项目

来自分类Dev

如何在每个项目中单击按钮删除项目?

来自分类Dev

重新加载页面后,将所选项目保留在导航栏中

来自分类Dev

将项目的最新版本保留在Python列表中

来自分类Dev

遍历文件并将数据保留在列表中-Python

来自分类Dev

合并多个地图并将值保留在列表中

来自分类Dev

如何修复QHBoxLayout项目大小并将下拉列表添加到QHBoxLayout中的每个项目

来自分类Dev

如何在列表中的每个项目上添加数字?

来自分类Dev

如何在MS Access中使用VBA在列表框中添加新项目

来自分类Dev

如何将内容放在按钮模板的中心并将鼠标事件保留在空白区域

来自分类Dev

如何将内容放在按钮模板的中心并将鼠标事件保留在空白区域

来自分类Dev

设备旋转后,如何将以编程方式创建的UIButton保留在屏幕底部?

来自分类Dev

用户单击列表视图项目后如何显示广告,但不是单击Android中的每个项目,如何显示广告?

来自分类Dev

如何从ListView项目单击事件显示的对话框中的每个肯定按钮启动不同的活动

来自分类Dev

Vue.js:添加新项目后从列表中删除项目导致错误

来自分类Dev

单击按钮后如何删除列表框的选定项目?

来自分类Dev

OpenWeather Api将JavaScript提取与用户输入配合使用,将以前的数据保留在HTML页面中

来自分类Dev

创建一个新项目并将其添加到我的邻居的列表中

来自分类Dev

如何在单击按钮时将项目添加到列表视图?

来自分类Dev

将项目的检查状态保留在搜索C#Winforms的复选框列表中

Related 相关文章

  1. 1

    如何在列表中添加新项目?

  2. 2

    动态刷新后如何将旧项目保留在列表视图中?

  3. 3

    WPF ComboBox 项目在 Visibility.Collapsed 后保留在列表中

  4. 4

    如何在将单元保留在项目中的同时从单元中删除表单?

  5. 5

    如何在按钮单击事件上将项目添加到列表视图?

  6. 6

    Android:将新项目添加到列表后,在ListView中单击NullPointerException onItemClick

  7. 7

    将XElement添加到列表中,并将其子元素保留在列表中

  8. 8

    每次单击按钮时,如何在列表框中添加项目?

  9. 9

    Angular 2 - 添加新项目后如何反转 Firebase 列表

  10. 10

    如何在Android Xamarin Visual Studio中单击按钮上的MvxListView上显示新项目

  11. 11

    如何在每个项目中单击按钮删除项目?

  12. 12

    重新加载页面后,将所选项目保留在导航栏中

  13. 13

    将项目的最新版本保留在Python列表中

  14. 14

    遍历文件并将数据保留在列表中-Python

  15. 15

    合并多个地图并将值保留在列表中

  16. 16

    如何修复QHBoxLayout项目大小并将下拉列表添加到QHBoxLayout中的每个项目

  17. 17

    如何在列表中的每个项目上添加数字?

  18. 18

    如何在MS Access中使用VBA在列表框中添加新项目

  19. 19

    如何将内容放在按钮模板的中心并将鼠标事件保留在空白区域

  20. 20

    如何将内容放在按钮模板的中心并将鼠标事件保留在空白区域

  21. 21

    设备旋转后,如何将以编程方式创建的UIButton保留在屏幕底部?

  22. 22

    用户单击列表视图项目后如何显示广告,但不是单击Android中的每个项目,如何显示广告?

  23. 23

    如何从ListView项目单击事件显示的对话框中的每个肯定按钮启动不同的活动

  24. 24

    Vue.js:添加新项目后从列表中删除项目导致错误

  25. 25

    单击按钮后如何删除列表框的选定项目?

  26. 26

    OpenWeather Api将JavaScript提取与用户输入配合使用,将以前的数据保留在HTML页面中

  27. 27

    创建一个新项目并将其添加到我的邻居的列表中

  28. 28

    如何在单击按钮时将项目添加到列表视图?

  29. 29

    将项目的检查状态保留在搜索C#Winforms的复选框列表中

热门标签

归档