通过绑定访问当前ItemsControl索引

时间:2016-11-12 21:47:41

标签: c# wpf data-binding itemscontrol stackpanel

我有以下代码

 <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&#39; s ItemsSource)并将其作为命令参数传递。有可能吗?

3 个答案:

答案 0 :(得分:0)

在创建<script type="text/javascript"> // Reload page 5 seconds after form submission $("form").onsubmit = setTimeout(function () { window.location = "/tradeshow/"; }, 5000); </script> 并发送此索引OnSelectOfferCommand时,可能适合您向每个index添加Offer属性。这将更容易

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

答案 1 :(得分:0)

是的,可以这样做。

<Button Command="{Binding SelectOfferCommand}"
CommandParameters="{Binding ElementName=ItemsControl,Path=SelectedIndex}"
/>

答案 2 :(得分:0)

您可以使用Index传递ItemsControl的当前AlterationIndex

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>