Andriy Shevchenko Andriy Shevchenko - 1 year ago 134
C# Question

Access current ItemsControl index via binding

I have the following piece of code

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

<TextBlock Text="{Binding CarDescription}" />
<Separator Height="1" Visibility="Hidden" />
<Button Content="Select"
Command="{Binding SelectOfferCommand}"/>


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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download