Reputation: 5627
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
Upvotes: 8
Views: 5475
Reputation: 1231
One solution is to simply do:
<ladder:LadderControl Participant="{Binding Path=.}"/>
{Binding Path=.}
should bind to the current element in the ItemsSource
list.
Upvotes: 0
Reputation: 20764
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; }
}
...
Upvotes: 1
Reputation: 18580
Just do the below, as the Participant is the context of each item
<ladder:LadderControl Participant="{Binding}"/>
Upvotes: 6