Home > OS >  WPF : how to add item in listview form textbox
WPF : how to add item in listview form textbox

Time:02-21

I have a problem in ListView. I want to add some text in ListView from a textbox but here I can add 1 item why? What I'm trying to do is

  • add text to a listview from a textbox
  • Load text from file.txt (like split) and put it into the listview

When you press the button, it is added once.

Here is the text stored in file.txt

Ice Cream|22|Canned Goods|50|Meat & Seafood|80

I want to split the line from file.txt and put it into a listview

Ice Cream|22

Ice Cream will be in the first place and 22 it will be in second place

enter image description here

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

Here is the source code:

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

I created a class called DataItems:

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

CodePudding user response:

I think there are 2 problems here:

First: when you load your items from your items.txt file you don't loop on items.

In your ReadTick method:

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

You just add in your list the first item. You should use a for loop to get all your items. For example:

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

and remove the second useless x .

As you can see I add 2 new properties:

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

And this is your Second problem: bindings.

Result:

enter image description here

  • Related