如何使用ItemSource将其他项添加到WPF ListBox?

时间:2021-04-16 19:39:14

I have a WPF form with a ListBox of items bound to a method that gets the items for the list box. What I want is an additional item at the top of the ListBox that behaves differently to the rest of the items.

我有一个WPF表单,其中ListBox的项目绑定到一个方法,该方法获取列表框的项目。我想要的是ListBox顶部的一个额外项目,其行为与其他项目不同。

For example, let's say I have a list of Authors (databound to a List), and when I select an author it populates another listbox with books from that author. I'd like to have an additional item in the lstAuthors listbox titled Show All, which will populate the other listbox with all books. How can I add this addtional item to my listbox?

例如,假设我有一个作者列表(数据绑定到列表),当我选择一个作者时,它会使用该作者的书籍填充另一个列表框。我想在lstAuthors列表框中添加一个标题为“全部显示”的附加项目,该列表将使用所有书籍填充其他列表框。如何将此附加项添加到列表框中?

1 个解决方案

#1


9  

Use CompositeCollection to merge your collection, with another collection containing your specialized item. Then, use a strongly typed datatemplate for your special select all option that does what you want in that section.

使用CompositeCollection将您的集合与包含您的专用项的另一个集合合并。然后,使用强类型数据模板作为特殊选择所有选项,在该部分中执行您想要的操作。

Other options include disabling scrolling in the list view, placing the listview inside a stackpanel, along with your select all, and then placing that stack panel in a scrollviewer, or just putting a button in the UI :)

其他选项包括在列表视图中禁用滚动,将listview放在stackpanel中,以及select all,然后将该堆栈面板放在scrollviewer中,或者只是在UI中放置一个按钮:)

#1


9  

Use CompositeCollection to merge your collection, with another collection containing your specialized item. Then, use a strongly typed datatemplate for your special select all option that does what you want in that section.

使用CompositeCollection将您的集合与包含您的专用项的另一个集合合并。然后,使用强类型数据模板作为特殊选择所有选项,在该部分中执行您想要的操作。

Other options include disabling scrolling in the list view, placing the listview inside a stackpanel, along with your select all, and then placing that stack panel in a scrollviewer, or just putting a button in the UI :)

其他选项包括在列表视图中禁用滚动,将listview放在stackpanel中,以及select all,然后将该堆栈面板放在scrollviewer中,或者只是在UI中放置一个按钮:)