C# 如何获取当前';项目';在ItemsSource绑定中
我有一个用户控件,它有一个依赖属性。在我的窗口中,我有一个对象列表,我正在创建一个由我的用户控件组成的统一网格。我正在将ItemsSource设置为对象列表,但我需要将每个对象传递给用户控件。请参阅下面的代码-我需要将参与者对象传递给LadderControlC# 如何获取当前';项目';在ItemsSource绑定中,c#,wpf,xaml,data-binding,itemssource,C#,Wpf,Xaml,Data Binding,Itemssource,我有一个用户控件,它有一个依赖属性。在我的窗口中,我有一个对象列表,我正在创建一个由我的用户控件组成的统一网格。我正在将ItemsSource设置为对象列表,但我需要将每个对象传递给用户控件。请参阅下面的代码-我需要将参与者对象传递给LadderControl <ItemsControl Grid.Row="2" Name="Participants" ItemsSource="{Binding Path=MyEvent.Participants}"> // more co
<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>
//这里有更多的代码,不相关
我有办法做到这一点吗?我应该考虑使用不同的模式吗
谢谢只需执行以下操作,因为参与者是每个项目的上下文
<ladder:LadderControl Participant="{Binding}"/>
您只需访问
LadderControl
中的DataContext
属性即可访问当前参与者
不需要单独的依赖项属性
class LadderControl
{
...
public IParticipant Participant
{
get{ return DataContext as IParticipant; }
}
...
一种解决方案是简单地执行以下操作:
<ladder:LadderControl Participant="{Binding Path=.}"/>
{Binding Path=.}
应该绑定到ItemsSource
列表中的当前元素。我认为这是最好的解决方案,因为LadderControl更易于移植。谢谢