Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between ItemTemplate and ItemContainerStyle in a WPF ListBox?

In WPF Listbox, I'm confused with these 2 notions: ItemTemplate and ItemContainerStyle Can someone explain me more?

like image 342
RHaguiuda Avatar asked May 14 '13 14:05

RHaguiuda


People also ask

What is ItemContainerStyle WPF?

WPF wpf. ListView ItemContainerStyle specifies a style that is used by every generated ListViewItem for styling it. You can create a Style inline or in the Resources section and set the ItemContainerStyle property of ListView. You can set ItemContainerStyle as StaticResource or DynamicResource.

What is a WPF ListBox?

ListBox is a control that provides a list of items to the user item selection. A user can select one or more items from the predefined list of items at a time. In a ListBox, multiple options are always visible to the user without any user interaction.


2 Answers

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:

 <!--Use the ItemTemplate to set a DataTemplate to define       the visualization of the data objects. This DataTemplate       specifies that each data object appears with the Proriity       and TaskName on top of a silver ellipse.-->   <ItemsControl.ItemTemplate>     <DataTemplate>       <DataTemplate.Resources>         <Style TargetType="TextBlock">           <Setter Property="FontSize" Value="18"/>           <Setter Property="HorizontalAlignment" Value="Center"/>         </Style>       </DataTemplate.Resources>       <Grid>         <Ellipse Fill="Silver"/>         <StackPanel>           <TextBlock Margin="3,3,3,0"                      Text="{Binding Path=Priority}"/>           <TextBlock Margin="3,0,3,7"                      Text="{Binding Path=TaskName}"/>         </StackPanel>       </Grid>     </DataTemplate>   </ItemsControl.ItemTemplate>   <!--Use the ItemContainerStyle property to specify the appearance       of the element that contains the data. This ItemContainerStyle       gives each item container a margin and a width. There is also       a trigger that sets a tooltip that shows the description of       the data object when the mouse hovers over the item container.-->   <ItemsControl.ItemContainerStyle>     <Style>       <Setter Property="Control.Width" Value="100"/>       <Setter Property="Control.Margin" Value="5"/>       <Style.Triggers>         <Trigger Property="Control.IsMouseOver" Value="True">           <Setter Property="Control.ToolTip"                   Value="{Binding RelativeSource={x:Static RelativeSource.Self},                           Path=Content.Description}"/>         </Trigger>       </Style.Triggers>     </Style>   </ItemsControl.ItemContainerStyle> 
like image 66
Esoteric Screen Name Avatar answered Oct 05 '22 01:10

Esoteric Screen Name


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.

like image 41
Jeff Avatar answered Oct 05 '22 02:10

Jeff