开发者

how can I add a special first item in wp7 listbox?

开发者 https://www.devze.com 2023-03-25 09:20 出处:网络
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 yo

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

0

精彩评论

暂无评论...
验证码 换一张
取 消