0

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?

James B
  • 8,975
  • 13
  • 45
  • 83

2 Answers2

1

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).

LPL
  • 16,827
  • 6
  • 51
  • 95
0

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

sac1
  • 1,344
  • 10
  • 15