James B
James B

Reputation: 9585

Different DataTemplates for ItemsControl WPF

I have defined an ItemsControl in XAML as:

<ItemsControl ItemsSource="{Binding MyCollection}"
              AlternationCount="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count}">

        <ItemsControl.Resources>
            <DataTemplate x:Key="TemplateOne">
                <Button Content="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Style="{StaticResource StyleOne}"/>
            </DataTemplate>
            <DataTemplate x:Key="TemplateTwo">
                <Button Content="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Style="{StaticResource StyleTwo}"/>
            </DataTemplate>
        </ItemsControl.Resources>

        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="ContentTemplate" Value="{StaticResource TemplateOne}"></Setter>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="ContentTemplate" Value="{StaticResource TemplateTwo}"></Setter>
                    </Trigger>
                </Style.Triggers>                    
            </Style>
        </ItemsControl.ItemContainerStyle>           

    </ItemsControl>

The idea being that I can set different templates based on the current alternation index of the ItemsControl. Whilst this works and gives me the different data templates, I also want the content of the Button to show its alternation index, i.e., the index of the item in MyCollection. Any ideas where I may be going wrong?

Upvotes: 0

Views: 417

Answers (2)

sac1
sac1

Reputation: 1344

You haven't got alternate style for all items. You have only two. I set ItemsControl Count to 2. So here is your ItemsControl (It includes @LPL's solution)

    <ItemsControl ItemsSource="{Binding MyCollection}"
          AlternationCount="2">

        <ItemsControl.Resources>
            <DataTemplate x:Key="TemplateOne">
                <Button Content="{Binding RelativeSource={RelativeSource AncestorType=ContentPresenter}, Path=(ItemsControl.AlternationIndex)}" Style="{StaticResource StyleOne}"/>
            </DataTemplate>
            <DataTemplate x:Key="TemplateTwo">
                <Button Content="{Binding RelativeSource={RelativeSource AncestorType=ContentPresenter}, Path=(ItemsControl.AlternationIndex)}" Style="{StaticResource StyleTwo}"/>
            </DataTemplate>
        </ItemsControl.Resources>

ItemsControl AlternationCount on MSDN

Upvotes: 0

LPL
LPL

Reputation: 17063

Try

<Button Content="{Binding RelativeSource={RelativeSource AncestorType=ContentPresenter},
                          Path=(ItemsControl.AlternationIndex)}"

as ItemsControl.AlternationIndex lives on ItemContainer (which is a ContentPresenter) not Button (in your example).

Upvotes: 1

Related Questions