tags:

views:

477

answers:

2

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?

A: 
YourListBox.Items.Insert(0, YourListItem);

I'm not sure on WPF, but this is the convention used throughout the rest of .Net, using the insert method allows you to specify the index in the collection to insert the new item.

DaRKoN_
No, WPF will throw an exception if you try to modify the Items list when the ItemsSource property has been set.
Matt Hamilton
+6  A: 

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

dhopton