What's the difference between ItemTemplate and ItemContainerStyle in a WPF ListBox?
WpfListboxItemtemplateItemcontainerstyleWpf Problem Overview
In WPF Listbox
, I'm confused with these 2 notions:
ItemTemplate
and ItemContainerStyle
Can someone explain me more?
Wpf Solutions
Solution 1 - Wpf
The ItemTemplate is for styling how the content of your data item appears. You use it to bind data fields, format display strings, and so forth. It determines how the data is presented.
The ItemContainerStyle is for styling the container of the data item. In a list box, this would be a ListBoxItem. Styling here affects things like selection behavior or background color. It determines style and UX of the display.
The MSDN page for ItemContainerStyle, linked above, has a pretty good example showing some differences:
>
Solution 2 - Wpf
The ItemContainerStyle just a wrapper for the DataTemplate so that a common item style can be applied to different data layouts.
Also, from this answer to "DataTemplate vs ItemContainerStyle":
> You can do all your styling in the ItemTemplate but the ItemContentStyle has VisualStates which control the Opacity on mouse over/disabled/selected etc. > > If you want to change those opacity state changes, or if you want any Container shape other than a rectangle, like a triangle for example, then you'll have to override the default ItemContainerStyle.