C# – How to add an additional item to a WPF ListBox using ItemSource

cnetwpfxaml

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.

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?

Best Answer

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.

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 :)