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

发布于 2024-11-25 10:38:06 字数 956 浏览 2 评论 0原文

我有一个 ItemsCountrol,其 ItemsSource 属性绑定到 ObservableCollection。我有一个显示此类型的用户控件(TeamUserControl)。我创建了一个数据模板,为 itemssource 集合中的每个自定义类型项加载此用户控件。此时,我在 TeamUserControl 中所做的任何 Binding 语句都可以通过路径 {Binding Path=TeamOwner} 直接引用团队属性并起作用。有没有办法将引用绑定到用户控件所代表的 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>

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2024-12-02 10:38:06

据我了解,您应该编写

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

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

我还建议您阅读 MSDN 文章,了解ItemsControl,然后查看 Binding 以明确您可以绑定到什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文