Search code examples
silverlight-4.0datatemplateitemcontainerstyle

DataTemplate vs ItemContainerStyle


I've seen a few other Silverlight 'vs' questions around, but couldn't find any for this particular match-up.

I'm trying to define the way in which my objects bound to a ListBox will display. I've defined a DataTemplate, but I'm really not sure where this should end and the ItemContainerStyle should begin.

Question 1: Is the ItemContainerStyle just a wrapper for the DataTemplate so that a common item style can be applied to different data layouts?

Question 1a: If so, in the event that a common item style isn't required, is the ItemContainerStyle even necessary or can all the layout and styling be defined in the DataTemplate?

Question 1b: If not, so what is it?

The ListBox is currently like this:

<ListBox Margin="40,118,41,61" ItemTemplate="{StaticResource TaskDataTemplate}"/>

The XAML for my DataTemplate is like this:

<DataTemplate x:Key="TaskDataTemplate">
        <Grid d:DesignHeight="95" Height="150">
            <StackPanel Margin="11,8,-10,68" Orientation="Horizontal" d:LayoutOverrides="Width">
                <TextBlock x:Name="TaskLabel" Margin="0,0,0,8" Style="{StaticResource TitleTextSmall}" TextWrapping="Wrap" Text="Task" VerticalAlignment="Stretch" d:LayoutOverrides="Height"/>
                <TextBlock x:Name="TaskID" HorizontalAlignment="Right" Margin="10,0,0,0" Style="{StaticResource TitleTextSmall}" TextWrapping="Wrap" Text="TaskID" VerticalAlignment="Stretch" d:LayoutOverrides="Height"/>
                <TextBlock x:Name="ChangeList" Style="{StaticResource NormalText}" TextWrapping="Wrap" Text="Changes..." Margin="30,2,0,0"/>
            </StackPanel>
            <ComboBox x:Name="TaskType" Style="{StaticResource TaskComboBox}" Height="29" VerticalAlignment="Top" Margin="131,30,16,0" d:LayoutOverrides="VerticalAlignment"/>
            <TextBlock x:Name="TaskTypeLabel" Margin="12,39,0,0" Style="{StaticResource NormalTextBold}" TextWrapping="Wrap" Text="Variation Reason" VerticalAlignment="Top" HorizontalAlignment="Left" Height="21"/>
            <TextBox x:Name="TaskDescription" Margin="12,70,15,11" TextWrapping="Wrap" Text="Enter description..." Style="{StaticResource TaskTextBox}" d:LayoutOverrides="VerticalAlignment"/>
        </Grid>
    </DataTemplate>

Thanks.


Solution

  • Answer 1: yes

    Answer 1a: as far as I can tell you can do all your styling in the ItemTemplate but the ItemContainerStyle 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.