简体   繁体   中英

How to display the content of time log in the WPF ListView?

I've been searching for answers but I haven't found one really. I know the logic but I just don't have the idea how to implement it.

This is my time log:

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

and I want to convert this into a listview. Really need your help.

For each entry create a class, something like this

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

(Just correct the names and types of properties).

Then you need to bind the collection of such entries to the ItemsSource of the ListView , and create a custom DataTemplate to display each entry. You can get some ideas in this tutorial .

If really need to display it as a grid, then consider using GridView instead.

UPDATE

Don't create and add list view items in the code behind of your view. In WPF you should follow the MVVM pattern. You can get started from here .

now i worked on the codes and i came up with this,

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"))
            ListView.Items.Add(new ListViewItem(line));
    }


    }

}

but an error occurred. it says, Error 1 'System.Windows.Controls.ListViewItem' does not contain a constructor that takes 1 arguments D:\\Documents\\Desktop\\textfile\\textfile\\MainWindow.xaml.cs 33 36 textfile

Here is my answer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM