Not all ListBoxItem are created equal

In the example below, the DataTemplate only gets applied to the first and second list item, the third and fourth are completely ignored.

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding}"></Button>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <sys:String>One</sys:String>
    <sys:String>Two</sys:String>
    <ListBoxItem>Three</ListBoxItem>
    <ListBoxItem>Four</ListBoxItem>
</ListBox>

My question is why?

If I look at the output window, I see the following error: “ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='ListBoxItem'.

So I get why my templates are not being applied, but why does WPF gets all nasty with me when I explicitly use ListBoxItems objects on my list items? I mean, WPF is implicitly hosting ALL items on a ListBoxItems object so why can’t WPF just thank me for doing some of the work that it is supposed to do rather than throwing the error? :)

Thank you.

1
задан Jay 4 September 2010 в 18:05
поделиться