I have a ListView with 3 headers, declared in XAML as follows:
我有一个带有3个头的ListView,在XAML中声明如下:
<ListView Name="myListView">
<ListView.View>
<GridView>
<GridViewColumn Header="H1"/>
<GridViewColumn Header="H2"/>
<GridViewColumn Header="H3"/>
</GridView>
</ListView.View>
</ListView>
I want to programmatically add a ListViewItem to this ListView, being able to set the content within the ListViewItem that will go under the first, second, and third columns individually. So far, I have only gotten this far:
我想以编程方式将ListViewItem添加到此ListView,能够在ListViewItem中设置将分别位于第一,第二和第三列下的内容。到目前为止,我只走到这一步:
ListViewItem l = new ListViewItem();
l.Content = "Content";
myListView.Items.Add(l);
This sets each column to the string "Content". How can I change the code above so that I can add a ListViewItem that will display "Content 1", "Content 2", and "Content 3" under the first, second, and third columns respectively? I tried to look for a SubItem or ListViewSubItem property within ListViewItem, but found nothing.
这会将每列设置为字符串“Content”。如何更改上面的代码,以便我可以添加一个ListViewItem,它将分别在第一,第二和第三列下显示“Content 1”,“Content 2”和“Content 3”?我试图在ListViewItem中查找SubItem或ListViewSubItem属性,但什么也没找到。
I assume there's a simple solution, but maybe I'm wrong. Please do not mention data binding, because I only want an answer to the question of programmatically setting the Content property to reflect individual changes in each column.
我假设有一个简单的解决方案,但也许我错了。请不要提及数据绑定,因为我只想回答以编程方式设置Content属性以反映每列中的各个更改的问题。
Thank you very much.
非常感谢你。
2 个解决方案
#1
12
This is not databinding. Think of the Binding statement as giving the column a name.
这不是数据绑定。可以将Binding语句视为为列命名。
<ListView Name="myListView">
<ListView.View>
<GridView>
<GridViewColumn Header="H1" DisplayMemberBinding="{Binding Col1}"/>
<GridViewColumn Header="H2" DisplayMemberBinding="{Binding Col2}"/>
<GridViewColumn Header="H3" DisplayMemberBinding="{Binding Col3}"/>
</GridView>
</ListView.View>
</ListView>
In code:
myListView.Items.Add(new { Col1 = "test1", Col2 = "Test2", Col3="test3"});
#2
0
In winforms here is what you do.
在winforms这里是你做的。
You have to set the column headins first, otherwise nothing will show. The add the list view items using a string array.
您必须先设置列headins,否则不会显示任何内容。使用字符串数组添加列表视图项。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
listView1.View=View.Details;
//Set Columns
listView1.Columns.Add("Col1");
listView1.Columns.Add("Col2");
listView1.Columns.Add("Col3");
//Fill Rows
ListViewItem lvi;
lvi=new ListViewItem(new string[] { "A", "B", "C" });
listView1.Items.Add(lvi);
lvi=new ListViewItem(new string[] { "D", "E", "F" });
listView1.Items.Add(lvi);
lvi=new ListViewItem(new string[] { "G", "H", "I" });
listView1.Items.Add(lvi);
}
}
a screenshot of the result is
结果的截图是
#1
12
This is not databinding. Think of the Binding statement as giving the column a name.
这不是数据绑定。可以将Binding语句视为为列命名。
<ListView Name="myListView">
<ListView.View>
<GridView>
<GridViewColumn Header="H1" DisplayMemberBinding="{Binding Col1}"/>
<GridViewColumn Header="H2" DisplayMemberBinding="{Binding Col2}"/>
<GridViewColumn Header="H3" DisplayMemberBinding="{Binding Col3}"/>
</GridView>
</ListView.View>
</ListView>
In code:
myListView.Items.Add(new { Col1 = "test1", Col2 = "Test2", Col3="test3"});
#2
0
In winforms here is what you do.
在winforms这里是你做的。
You have to set the column headins first, otherwise nothing will show. The add the list view items using a string array.
您必须先设置列headins,否则不会显示任何内容。使用字符串数组添加列表视图项。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
listView1.View=View.Details;
//Set Columns
listView1.Columns.Add("Col1");
listView1.Columns.Add("Col2");
listView1.Columns.Add("Col3");
//Fill Rows
ListViewItem lvi;
lvi=new ListViewItem(new string[] { "A", "B", "C" });
listView1.Items.Add(lvi);
lvi=new ListViewItem(new string[] { "D", "E", "F" });
listView1.Items.Add(lvi);
lvi=new ListViewItem(new string[] { "G", "H", "I" });
listView1.Items.Add(lvi);
}
}
a screenshot of the result is
结果的截图是