简体   繁体   English

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

[英]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): 我想以编程方式创建一个这样的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          |
|-----------------------------------------------------------|

I can't find much on this, and here is what I'm stuck with right now (I am using WPF by the way): 我在这方面找不到很多东西,这就是我现在所坚持的(顺便说一句,我正在使用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);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 和 WPF DataGrid 以编程方式创建 N 个列 - How to create N number of columns programmatically using C# with WPF DataGrid 如何创建特定号的ListView。 WPF中的列? - How can i create a ListView of specific no. of columns in WPF? 如何以编程方式滚动 WPF 列表视图? - How can I programmatically scroll a WPF listview? 如何在代码(C#和WPF)中以动态方式和编程方式更新具有更多行和列的列表框中的网格 - How to update a grid in a listbox with more rows and columns dynamically and programmatically in code (C# and WPF) 如何在C#中使用Deedle从帧中仅选择包含非负值的列? - How can I select only the columns containing non negative values from a frame using Deedle in C#? 您如何在C#中以编程方式为WPF工具包datagrid创建列? - How do you create columns programmatically in C# for a WPF toolkit datagrid? 如何对包含字符串和整数的Listview列进行排序? - How to Sort Listview Columns Containing Strings & Integers? WPF如何编辑以编程方式添加的DataGrid中的行 - WPF how to I edit rows in a DataGrid that I added programmatically C# 如何以编程方式在C#中创建字符串? - How can I create a string in C# programmatically? 如何在 C# 窗口应用程序中以编程方式创建按钮? - How can I create a button programmatically in C# window app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM