绑定(bind)语法 {Binding/>
在 WPF 中有效,但在 Silverlight 3 中根本无效:
<ContentControl Content="{Binding MyCollection}">
<ContentControl.ContentTemplate>
<DataTemplate>
<ContentControl Content="{Binding /}" />
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
在 Silverlight 中解决这个问题的方法是什么?
最佳答案
在 WPF 中绑定(bind)到集合时,实际上是绑定(bind)到理解“当前项”概念的对象。
但是在 Silverlight 中,您要绑定(bind)的对象没有这个概念。所以你需要自己做。
例如,在 MVVM 应用程序中公开一个属性。
<ListBox SelectedItem="{Binding MyCurrentItem}"
ItemsSource="{Binding MyCollection}"/>
<ContentControl Content="{Binding MyCurrentItem}" />
或者做一些元素绑定(bind)
<ListBox x:Name="listBox"
ItemsSource="{Binding MyCollection}"/>
<ContentControl Content="{Binding SelectedItem, ElementName=listBox}" />
关于wpf - Silverlight 3 绑定(bind)到集合中的当前项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2573796/