The listbox has a datatemplate, but I want add a special item to the listbox, which can not be pro开发者_如何学Gocessed by the datatemplate. How can I do this?
If you have the CompositeCollection
at your disposal (- sorry if you don't -) you could try to work with that.
private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
var specialItem = new ListBoxItem()
{
Content = "Very special item.",
FontWeight = FontWeights.Bold
};
var collectionContainer = new CollectionContainer()
{
Collection = MyCollection
};
var composite = new CompositeCollection();
composite.Add(specialItem);
composite.Add(collectionContainer);
(sender as ListBox).ItemsSource = composite;
}
If your Listbox has a reasonable number of items, you can set the AlternationCount
of the Listbox to something greater than your item count, and use a DataTrigger on the ListBoxItem's AlternationIndex
to do something special such as switching templates if it's equal to 0
精彩评论