设置 DataTemplate 控件属性绑定到 ItemsSource 集合中的项目?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,您应该编写
{Binding}
语句来绑定到 ItemsSource 中的当前项目,其中类型T
是您的ObservableCollection的类型。 League.Teams
使用。我还建议您阅读 MSDN 文章,了解ItemsControl,然后查看
Binding
以明确您可以绑定到什么。As I understand you, you should to write
{Binding}
statement will bind to current item in ItemsSource, where typeT
is type which yourObservableCollection<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.