1

I have the following piece of code

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

This produces a bunch of repeated boxes, every has a button. Every time i click the button i want to access current box index (from ItemsControl's ItemsSource) and pass it as a command parameter. Is it possible to do it?

Andriy Shevchenko
  • 1,117
  • 6
  • 21
  • 3
    Why? You can pass the actual item that the itemTemplate is bound to. – XAMlMAX Nov 12 '16 at 21:52
  • Don't naming visual object as it's type. You should change the name of your ItemsControl to another name eg: "ItemsControlOffers". – Red Wei Nov 13 '16 at 10:59
  • @UçanKartal For what reason? "ItemsControl" is a perfectly valid name here. – Clemens Nov 13 '16 at 20:22
  • @Clemens Because when you have a lot of windows, it will help you to distinct between the entities. – Red Wei Nov 14 '16 at 06:45
  • @UçanKartal That doesn't make sense. There is certainly only one window here. – Clemens Nov 14 '16 at 06:53
  • Ok, i figured it out `Command= "{Binding ElementName=ItemsControlOffers, Path=DataContext.OnSelectOfferCommand}" CommandParameter="{Binding Index}"` I added an Index property to each Offer and passed it as command parameter – Andriy Shevchenko Nov 14 '16 at 13:30

2 Answers2

4

You can pass the current Index of an ItemsControl using the AlterationIndex.

See more info here

Example:

<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>
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • Don't do this, see https://stackoverflow.com/questions/6511180/wpf-itemscontrol-the-current-listitem-index-in-the-itemssource/17962582#17962582. – huang Apr 16 '21 at 11:33
0

May be it will be suitable for you to add index property to each Offer while creating Offers and send this index OnSelectOfferCommand. It will be much easier

ps I think i must explain my answer: My sugestion is not only easier in realisation, but also it is a good practice to seperate busines logic from UI. In this case if UI will be changed, changes will not effect whole ordering process