不使用 XAML 将子项添加到 ListView
如何向 ListView
添加子项?我需要动态生成所有内容,但我找到的每个示例都使用 XAML。
非 WPF 是如此简单:
ListViewItem lvi = listview.items.add(wahtever);
lvi. blah blah blah
如何在不使用 XAML 的情况下在 WPF 中添加子项?
How do you add sub-items to a ListView
? I need to generate everything dynamically, but every example I've found uses XAML.
Non-WPF was so simple:
ListViewItem lvi = listview.items.add(wahtever);
lvi. blah blah blah
How do you add sub-items in WPF without using XAML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如已经提到的,WPF 没有像 WinForms 这样的子项。相反,您可以使用适合您目的的对象的属性。
为了完整起见,下面是 XAML 与代码的对比。
XAML:
代码:
As already mentioned, WPF doesn't have sub-items like WinForms. Instead you use properties on an object that suits your purposes.
For completeness, here is XAML contrasted with code.
XAML:
Code:
“WPF 方式”是将列表视图绑定到表示要显示的数据的集合。然后将包含数据的对象添加到该集合中。您几乎不需要像您计划的那样手动将
ListViewItems
添加到列表中。我可以举一个例子,但是这里已经有很多线程可以解决这个问题:The "WPF way" would be to bind your listview to a collection that represents the data you want to display. Then add objects containing data to that collection. You almost never should have to deal with adding
ListViewItems
to your list manually as you are planning to do. I could come up with an example but there's many threads here on SO already that solve exactly this problem:子项还是列表项??
简单:
您可以将任何类型的对象添加到该集合或特定的 ListBoxItem 对象中,如下所示:
SubItem or ListItem??
Easy:
You can add any type of object into that collection or a specific ListBoxItem object like this one: