通过绑定访问当前 ItemsControl 索引

Access current ItemsControl index via binding

我有如下一段代码

 <ItemsControl x:Name="ItemsControl" ItemsSource="{Binding Offers}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Vertical"></StackPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <wpf:Card Padding="32" Margin="5" d:DataContext="{d:DesignData }">
                            <StackPanel Margin="0,0,0,-30" Height="107">
                                <TextBlock
                                    Style="{DynamicResource MaterialDesignTitleTextBlock}">
                                    <Run Text="Offer " />

                                </TextBlock>
                                <TextBlock Text="{Binding CarDescription}" />
                                <Separator Height="1" Visibility="Hidden" />
                            <Button Content="Select" 
                                        Width="72" 
                                        VerticalAlignment="Bottom" 
                                        HorizontalAlignment="Right"
                                        Margin="0,20,0,0"
                                        Command="{Binding SelectOfferCommand}"/>
                        </StackPanel>
                        </wpf:Card>

                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

这会产生一堆重复的框,每个框都有一个按钮。每次单击按钮时,我都想访问当前框索引(来自 ItemsControl 的 ItemsSource)并将其作为命令参数传递。可以吗?

可能适合您在创建 Offers 时将 index 属性 添加到每个 Offer 并发送此索引 OnSelectOfferCommand。会容易很多

ps 我想我必须解释一下我的回答:我的建议不仅更容易实现,而且将业务逻辑与 UI 分开也是一个很好的做法。在这种情况下,如果 UI 将被更改,更改将不会影响整个订购过程

您可以使用 AlterationIndex.

传递 ItemsControl 的当前 Index

See more info here

示例:

<ItemsControl x:Name="ItemsControl" 
              ItemsSource="{Binding Offers}"
              AlternationCount="1000">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical"></StackPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <wpf:Card Padding="32" Margin="5" d:DataContext="{d:DesignData }">
                <StackPanel Margin="0,0,0,-30" Height="107">
                    <TextBlock
                        Style="{DynamicResource MaterialDesignTitleTextBlock}">
                        <Run Text="Offer " />

                    </TextBlock>
                    <TextBlock Text="{Binding CarDescription}" />
                    <Separator Height="1" Visibility="Hidden" />
                <Button Content="Select" 
                        Width="72" 
                        VerticalAlignment="Bottom" 
                        HorizontalAlignment="Right"
                        Margin="0,20,0,0"
                        Command="{Binding SelectOfferCommand}"
                        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}"/>
                </StackPanel>
            </wpf:Card>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>