The listbox has a datatemplate, but I want add a special item to the listbox, which can not be processed 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;
}
如果您的列表框具有合理数量的项目,则可以将列表框的AlternationCount
设置为大于您的项目计数,并在ListBoxItem的AlternationIndex
上使用DataTrigger进行某些特殊操作,例如,如果它等于0,则切换模板
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.