我在ListView. 我想ListView從文本框中添加一些文本,但在這里我可以添加 1 項,為什么?我想做的是
- 從文本框中將文本添加到串列視圖
- 從(如拆分)加載文本
file.txt并將其放入串列視圖
當您按下按鈕時,它會被添加一次。
這是存盤在 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:
namespace Wpf_ListView
{
public class DataItems
{
public string NameItem { get; set; }
public string ItemFast { get; set; }
}
}
uj5u.com熱心網友回復:
我認為這里有兩個問題:
第一:當您從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; }
這是您的第二個問題:系結。
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/430523.html
