如何在ItemsSource绑定中获取当前的“item”

8

我有一个用户控件,里面有一个依赖属性。在我的窗口中,我有一个对象列表,并创建了一个由我的用户控件组成的均匀网格。我将ItemsSource设置为我的对象列表,但需要将每个对应的对象传递给用户控件。请参见下面的代码 - 我需要将Participant对象传递给LadderControl。

<ItemsControl Grid.Row="2" Name="Participants" ItemsSource="{Binding Path=MyEvent.Participants}">

    // more code here, irrelevant

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ladder:LadderControl Participant="CURRENT_ITEM_IN_PARTICIPANTS_LIST"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

有没有办法我可以做到这一点?我应该考虑使用不同的模式吗?

谢谢

3个回答

5
只需按照以下步骤操作,因为参与者是每个项目的上下文。
<ladder:LadderControl Participant="{Binding}"/>

我认为这是最好的解决方案,因为LadderControl更加便携。谢谢。 - Sherlock

1
你可以简单地访问LadderControl中的DataContext属性来访问当前参与者。
不需要单独的依赖属性。
  class LadderControl 
  {
       ...
       public IParticipant Participant 
       {
            get{ return DataContext as IParticipant; } 
       }
       ...

0
一种解决方案是直接这样做:

        <ladder:LadderControl Participant="{Binding Path=.}"/>

{Binding Path=.} 应该绑定到 ItemsSource 列表中的当前元素。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接