0
votes

I have an ItemsCountrol with its ItemsSource property bound to an ObservableCollection. I have a usercontrol (TeamUserControl) that displays this type. I created a datatemplate that loads this usercontrol for each customtype item in the itemssource collection. At this point any Binding statements I make inside TeamUserControl can reference Team properties directly by path {Binding Path=TeamOwner} and work. Is there a way to bind a reference to the ItemsSource item the usercontrol is representing? For example, in the TeamUserControl creating a dependancy property of type Team and have it bound to the instance of the item from the ItemsSource.

    <ItemsControl Name="ItemCtrl" Grid.Row="0" ItemsSource="{Binding Path=League.Teams}">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <mycontrols:TeamUserControl AttachedTeam="{Binding ???}" TeamOwnerName="{Binding Path=TeamOwner}"/>
        </DataTemplate> 
      </ItemsControl.ItemTemplate>
    </ItemsControl>

In this example the window is representing a class "League" which has the property: ObservableCollection Teams. And there is a class "Team" which has the property:TeamOwner. The TeamUserControl has two dependancy properties: AttachedTeam of type Team, and TeamOwnerName of type string.

I included the team-owner property reference to show that there is an instance of Team for each of these usercontrols. I am just not sure how to reference it.

1

1 Answers

5
votes

As I understand you, you should to write

<mycontrols:TeamUserControl AttachedTeam="{Binding}" TeamOwnerName="{Binding Path=TeamOwner}"/>

{Binding} statement will bind to current item in ItemsSource, where type T is type which your ObservableCollection<T> League.Teams uses.

I also recommend you to read MSDN article about ItemsControl, and look around Binding to make it clear for yourself, what you can bind to.