次の XAML を検討してください
<ItemsControl ItemsSource="{Binding Path=MyItems, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" AlternationCount="999" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex),
RelativeSource={RelativeSource TemplatedParent},
FallbackValue=FAIL,
StringFormat={}Index is {0}}" />
<ItemsControl ItemsSource="{Binding Path=MySubItems, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=1}, Path=(ItemsControl.AlternationIndex)}"/>
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=2}, Path=(ItemsControl.AlternationIndex)}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
3 つの TextBlock ノードがあります。1 番目の TextBlock は 1 番目の ItemsControl の直接の子であり、予想どおり AlternationIndex を示します。ただし、2 番目の ItemsControl で、その AlternationIndex をさらに深くする必要があります。したがって、私は TemplatedParent を使用できず、AncestorLevel で祖先を見つけることができると考えました。ただし、2 番目の ItemsControl の TextBlock ノードは両方とも "0" を示します。
私は何が欠けていますか?2 番目の ItemsControl 内から 1 番目の ItemsControl をターゲットにするにはどうすればよいですか?