设置DataTemplate控件属性绑定到ItemsSource集合中的项目吗?

0 wpf datatemplate itemtemplate itemssource

我有一个ItemsCountrol,它的ItemsSource属性绑定到ObservableCollection。我有一个显示此类型的用户控件(TeamUserControl)。我创建了一个数据模板,用于为itemssource集合中的每个customtype项加载此用户控件。此时,我在TeamUserControl中所做的任何Binding语句都可以直接通过路径{Binding Path = TeamOwner}引用Team属性并开始工作。有没有一种方法可以将引用绑定到用户控件代表的ItemsSource项目?例如,在TeamUserControl中创建Team类型的依赖项属性,并将其绑定到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>
Run Code Online (Sandbox Code Playgroud)

在此示例中,窗口表示具有属性:ObservableCollection Teams的“联盟”类。并且有一个类“ Team”,其属性为:TeamOwner。TeamUserControl具有两个从属属性:Team类型的AttachedTeam和string类型的TeamOwnerName。

我包括了team-owner属性引用,以显示每个这些用户控件都有一个Team实例。我只是不确定如何引用它。

stu*_*bax 5

据我了解,您应该写

<mycontrols:TeamUserControl AttachedTeam="{Binding}" TeamOwnerName="{Binding Path=TeamOwner}"/>
Run Code Online (Sandbox Code Playgroud)

{Binding}语句将绑定到ItemsSource中的当前项目,其中type T是您ObservableCollection<T> League.Teams使用的类型。

我还建议您阅读有关ItemsControl的MSDN文章,并环顾四周Binding以明确自己可以绑定的内容。