It sounds more like a warning than error -- just filling you in on why your declared ItemTemplate
and ListBoxItem
s aren't going to work together.
edit
In response to your comment, consider the following:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<System:String>One</System:String>
<System:String>Two</System:String>
<ListBoxItem Background="Red">Three</ListBoxItem>
<ListBoxItem>
<ListBoxItem.Template>
<ControlTemplate>
<Button Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"
Background="Blue"
Foreground="White"
/>
</ControlTemplate>
</ListBoxItem.Template>
<ListBoxItem.Content>
Four
</ListBoxItem.Content>
</ListBoxItem>
</ListBox.Items>
</ListBox>
Think of the ItemTemplate
as defining a way to display the item type. For items "One" and "Two", which are strings, the ItemTemplate
defines how to display that and wraps the result in a ListBoxItem
.
If, however, an item is already a ListBoxItem
- This is not an item that the
ItemTemplate
can necessarily deal with because an ItemTemplate
is a DataTemplate
, and
- The
ListBoxItem
can define its own properties, style, and template.
So, if you are explicitly creating ListBoxItem
s, I think it is better that WPF respect the ability to customize the styling of that item, rather than overriding it all with the ItemTemplate
.
That's the beauty of using ItemTemplates
-- you can use them, mix, match, and mangle them, usually without really having to worry about ListBoxItem
. The only time I've ever had to deal with ListBoxItem
directly is when I create a style for it -- declaring one as an item explicitly is probably a rare case at best.