Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current 'item' in an ItemsSource binding

I have a user control that has one dependency property. In my window I have a list of objects, and I am creating a uniform grid consisting of my user control. I am setting the ItemsSource to my list of objects, but I need to pass each respective object to the user control. Please see the code below - I need to pass in the Participant object to the LadderControl.

<ItemsControl Grid.Row="2" Name="Participants" ItemsSource="{Binding Path=MyEvent.Participants}">

    // more code here, irrelevant

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ladder:LadderControl Participant="CURRENT_ITEM_IN_PARTICIPANTS_LIST"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Is there a way I can do this ? Should I be thinking about using a different pattern ?

Thanks

like image 916
Sherlock Avatar asked Sep 25 '13 12:09

Sherlock


2 Answers

Just do the below, as the Participant is the context of each item

<ladder:LadderControl Participant="{Binding}"/>
like image 73
Nitin Avatar answered Nov 15 '22 19:11

Nitin


You can simply access the DataContext Property in LadderControl to access the currrent participant.

There is no need for a separate dependency property.

  class LadderControl 
  {
       ...
       public IParticipant Participant 
       {
            get{ return DataContext as IParticipant; } 
       }
       ...
like image 24
thumbmunkeys Avatar answered Nov 15 '22 20:11

thumbmunkeys