如何以编程方式添加多列 ListViewItem 而无需任何数据绑定?
我有一个带有 3 个标头的 ListView,在 XAML 中声明如下:
<ListView Name="myListView">
<ListView.View>
<GridView>
<GridViewColumn Header="H1"/>
<GridViewColumn Header="H2"/>
<GridViewColumn Header="H3"/>
</GridView>
</ListView.View>
</ListView>
我想以编程方式将 ListViewItem 添加到此 ListView,从而能够设置 ListViewItem 中分别位于第一列、第二列和第三列下的内容。到目前为止,我只得到了这一点:
ListViewItem l = new ListViewItem();
l.Content = "Content";
myListView.Items.Add(l);
这将每一列设置为字符串“Content”。如何更改上面的代码,以便添加一个 ListViewItem,该 ListViewItem 将分别在第一列、第二列和第三列下显示“内容 1”、“内容 2”和“内容 3”?我尝试在 ListViewItem 中查找 SubItem 或 ListViewSubItem 属性,但什么也没找到。
我认为有一个简单的解决方案,但也许我错了。请不要提及数据绑定,因为我只想回答以编程方式设置 Content 属性以反映每列中的单独更改的问题。
非常感谢。
I have a ListView with 3 headers, declared in XAML as follows:
<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 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.
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.
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是数据绑定。将 Binding 语句视为为列命名。
在代码中:
This is not databinding. Think of the Binding statement as giving the column a name.
In code:
在 winforms 中,您将执行以下操作。
您必须先设置列标题,否则将不会显示任何内容。使用字符串数组添加列表视图项目。
结果的屏幕截图为
In winforms here is what you do.
You have to set the column headins first, otherwise nothing will show. The add the list view items using a string array.
a screenshot of the result is