Question

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

Was it helpful?

Solution

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

<ladder:LadderControl Participant="{Binding}"/>

OTHER TIPS

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; } 
       }
       ...

One solution is to simply do:

        <ladder:LadderControl Participant="{Binding Path=.}"/>

{Binding Path=.} should bind to the current element in the ItemsSource list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top