简体   繁体   English

绑定到ItemsControl中ItemsControl的AlternateIndex

[英]Bind to AlternationIndex of ItemsControl in ItemsControl

Consider the following XAML 考虑以下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>

There are three TextBlock nodes. 有三个TextBlock节点。 The 1st TextBlock is a direct child of the 1st ItemsControl, and shows the AlternationIndex, as expected. 第一个TextBlock是第一个ItemsControl的直接子级,并且按预期显示了AlternateIndex。 However, I need that AlternationIndex a level deeper, in the second ItemsControl. 但是,在第二个ItemsControl中,我需要更深一层的AlternateIndex。 Therefore I can't use TemplatedParent and thought I could find the Ancestor with AncestorLevel. 因此,我无法使用TemplatedParent,并认为可以使用AncestorLevel找到祖先。 However, both TextBlock nodes in the 2nd ItemsControl show "0". 但是,第二个ItemsControl中的两个TextBlock节点都显示为“ 0”。

What I'm I missing? 我想念的是什么? How do I target the 1st ItemsControl from within the 2nd ItemsControl? 如何从2nd ItemsControl中定位1st ItemsControl?

The AlternationIndex won't be on the ItemsControl but on each of its children. AlternateIndex不会在ItemsControl上,而是在其每个子控件上。 Using DataTemplate your ItemsControl will store each children in a ContentPresenter which will have the AlternationIndex. 使用DataTemplate,ItemsControl会将每个子级存储在ContentAlsenter中,该ContentPresenter具有AlternateIndex。 What you need to modify: 您需要修改的内容:

<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}, AncestorLevel=2}, Path=(ItemsControl.AlternationIndex)}"/>
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}, AncestorLevel=2}, Path=(ItemsControl.AlternationIndex)}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM