如何将ListBox项绑定到用户控件?

8

人们经常使用类似以下内容的语句:

<ListBox ItemsSource="{Binding ElementName=thisControl, Path=ListIndexes}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Path=IndexName}"/>
<Label Content="{Binding Path=IndexValue}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

但我想使用一个控件来代替标签,就像这样:

<ListBox ItemsSource="{Binding ElementName=thisControl, Path=ListIndexes}">
<ListBox.ItemTemplate>
<DataTemplate>
<local:Index Item="**{Binding}**"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

我的疑问是在这个绑定中放什么才能包含集合中的整个项目。

1个回答

15
这个的语法是:

这是语法:

<local:Index Item="{Binding}"/>

这将告诉数据绑定函数将每个ListBox项的整个数据上下文绑定到您的Index控件中的Item属性


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