如何使用C#以编程方式在WPF中以编程方式创建包含包含列和行的字符串的ListView?

时间:2013-04-29 19:51:44

标签: c# wpf listview

我想以编程方式创建一个像这样的ListView(我知道有更好的控件可以做到这一点,但我不想使用其他控件,因为我只想测量执行此操作的性能列表视图):

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

我在这方面找不到多少,这就是我现在所困扰的东西(我顺便使用WPF):

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

1 个答案:

答案 0 :(得分:0)

使用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;