1

我有一个 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>

在此示例中,该窗口表示具有以下属性的“联盟”类:ObservableCollection Teams。还有一个“团队”类,它具有以下属性:TeamOwner。TeamUserControl 有两个依赖属性:Team 类型的 AttachedTeam 和字符串类型的 TeamOwnerName。

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

4

1 回答 1

6

据我了解,你应该写

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

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

我还建议您阅读有关 ItemsControl 的MSDN文章,并环顾四周Binding以自己清楚,可以绑定到什么。

于 2011-07-21T08:58:13.673 回答