简体   繁体   中英

How can I programmatically create a ListView full of strings containing columns and rows in WPF using C#?

I would like to create a ListView like this programmatically (I am aware there are better controls out there to do this, but I do not wish to use other controls, because I just want to measure the performance of doing this with a list view):

|-----------------------------------------------------------|
|Column One Header  |Column 2 Header  |Column Three Header  |
|-----------------------------------------------------------|
|Cell Text 1        |Cell Text 2      |Cell Text 3          |
|-----------------------------------------------------------|
|Cell Text 4        |Cell Text 5      |Cell Text 6          |
|-----------------------------------------------------------|

I can't find much on this, and here is what I'm stuck with right now (I am using WPF by the way):

ListView listView = new ListView();
listView.Height = 203;
listView.Width = 501;
listView.Margin = new Thickness(0, 0, 0, 0);
Grid.SetRow(listView, 1);
Grid.SetColumn(listView, 0);
mainGrid.Children.Add(listView);

Use GridView

GridView myGridView = new GridView();
myGridView.AllowsColumnReorder = true;

GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("ColumnOneHeader");
gvc1.Header = "Column One Header";
gvc1.Width = 100;
myGridView.Columns.Add(gvc1);
GridViewColumn gvc2 = new GridViewColumn();
gvc2.DisplayMemberBinding = new Binding("Column2Header");
gvc2.Header = "Column 2 Header";
gvc2.Width = 100;
myGridView.Columns.Add(gvc2);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.DisplayMemberBinding = new Binding("ColumnThreeHeader");
gvc3.Header = "Column Three Header";
gvc3.Width = 100;
myGridView.Columns.Add(gvc3);


listView.View = myGridView;

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