WPF:如何在列表视图表单文本框中添加项目

WPF : how to add item in listview form textbox

我在 ListView 中遇到问题。我想从文本框中在 ListView 中添加一些文本,但在这里我可以添加 1 个项目,为什么?我想做的是

当你按下按钮时,它被添加一次。

这是存储在file.txt

中的文本
Ice Cream|22|Canned Goods|50|Meat & Seafood|80

我想从 file.txt 拆分行并将其放入列表视图

Ice Cream|22

Ice Cream会排在第一位22会排在第二位

<ListView x:Name="ListView1" ItemsSource="{Binding MyData}" HorizontalAlignment="Left" Height="240" Margin="20,100,0,0" VerticalAlignment="Top" Width="280" Background="#FF636363" Foreground="White">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding NameItem}" Width="210"/>
            <GridViewColumn Header="Number" DisplayMemberBinding="{Binding ItemFast}" Width="55"/>
        </GridView>
    </ListView.View>
</ListView>

这里是源代码:

using System;
using System.Collections.Generic;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.IO;

namespace Wpf_ListView
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        StreamReader SR;
        StreamWriter SW;

        public List<DataItems> MyData { get; set; }

        ListBox LB = new ListBox();

        System.Timers.Timer TimerReader = new System.Timers.Timer();

        string[] ReadAllText;
        string LoadText;

        int x = 0;

        string path = Environment.CurrentDirectory + @"\File\Data.txt";

        private void MainWindow1_Loaded(object sender, RoutedEventArgs e)
        {
            SR = new StreamReader(path);
            ReadAllText = SR.ReadToEnd().ToString().Split('|');
            SR.Close();

            foreach (var item in ReadAllText)
            {
                LB.Items.Add(item);
            }

            TimerReader.Elapsed += ReadTick;
            TimerReader.Interval = 100;
            TimerReader.Enabled = true;
        }

        private void ReadTick(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
           {
               try
               {
                   MyData = new List<DataItems>();
                   DataItems data = new DataItems();

                   data.NameItem = LB.Items[x].ToString();
                   x++;
                   data.ItemFast = LB.Items[x].ToString();
                   x++;

                   MyData.Add(data);
                   DataContext = this;

                   if (x == LB.Items.Count)
                   {
                       x = 0;
                       TimerReader.Enabled = false;
                   }
               }
               catch
               {

               }
           });
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MyData = new List<DataItems>();

            DataItems data = new DataItems();
            data.NameItem = ItemNames.Text; // textbox 1
            data.ItemFast = ItemPrices.Text; // textbox 2

            MyData.Add(data);
            DataContext = this;

            SR = new StreamReader(path);
            LoadText = SR.ReadToEnd(); // Here because the new text has been placed and the old one will be deleted, so I called it up again before deleting it
            SR.Close();

            SW = new StreamWriter(path);
            SW.Write(LoadText + ItemNames.Text + "|" + ItemPrices.Text + "|");
            SW.Close();
        }
    }
}

我创建了一个名为 DataItems 的 class:

namespace Wpf_ListView
{
    public class DataItems
    {
        public string NameItem { get; set; }
        public string ItemFast { get; set; }
    }
}

我认为这里有两个问题:

首先: 当您从 items.txt 文件加载项目时,您不会在项目上循环。

在您的 ReadTick 方法中:

MyData = new List<DataItems>();
DataItems data = new DataItems();

data.NameItem = LB.Items[x].ToString();
x++;
data.ItemFast = LB.Items[x].ToString();
x++;

MyData.Add(data);

您只需在列表中添加第一项。 您应该使用 for 循环来获取所有项目。 例如:

MyData = new List<DataItems>();
    
for (int x = 0; x < LB.Items.Count; x++) {
   DataItems data = new DataItems();
   NameItem = LB.Items[x].ToString();
   data.NameItem = NameItem;
   x++;
   ItemFast = LB.Items[x].ToString();
   data.ItemFast = ItemFast;
    
   MyData.Add(data);
}

并删除第二个无用的x++

如您所见,我添加了 2 个新属性:

public string NameItem { get; set; }
public string ItemFast { get; set; }

这是您的第二个问题:绑定。

结果: