[英]How to display the content of time log in the WPF ListView?
我一直在尋找答案,但實際上還沒有找到答案。 我知道邏輯,但是我不知道如何實現。
這是我的時間日志:
No Mchn EnNo Name Mode IOMd DateTime
00001 1 00001234 1 0 2004/01/01 01:01
00002 1 00001234 1 0 2015/05/12 04:01
00003 1 00001234 2 0 2015/05/12 04:01
00004 1 00001234 1 0 2004/01/01 01:01
00005 1 00001234 1 0 2015/05/13 10:42
我想將其轉換為列表視圖。 確實需要您的幫助。
為每個條目創建一個類,如下所示
internal class TimeLogEntryViewModel
{
public string Id { get; set; }
public string Mchn { get; set; }
public string EnNo { get; set; }
public string Name { get; set; }
public string Mode { get; set; }
public string IOMd { get; set; }
public DateTime DateTime { get; set; }
}
(只需更正屬性的名稱和類型)。
然后,您需要將此類條目的集合綁定到ListView
的ItemsSource
,並創建一個自定義DataTemplate
來顯示每個條目。 您可以在本教程中獲得一些想法。
如果確實需要將其顯示為網格,請考慮改用GridView
。
更新
不要在視圖后面的代碼中創建和添加列表視圖項。 在WPF中,您應該遵循MVVM模式。 您可以從這里開始。
現在我從事代碼工作,我想到了這一點,
命名空間文本文件{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void LoadButton(object sender, RoutedEventArgs e)
{
foreach (string line in File.ReadAllLines(@"GLG_001.TXT"))
ListView.Items.Add(new ListViewItem(line));
}
}
}
但是發生了錯誤。 它說,錯誤1'System.Windows.Controls.ListViewItem'不包含帶有1個參數的構造函數D:\\ Documents \\ Desktop \\ textfile \\ textfile \\ MainWindow.xaml.cs 33 36文本文件
這是我的答案:
namespace textfile
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void LoadButton(object sender, RoutedEventArgs e)
{
foreach (string line in File.ReadAllLines(@"GLG_001.TXT"))
{
var listViewItem = new ListViewItem();
listViewItem.Content = line;
ListView.Items.Add(listViewItem);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.