wpf - Silverlight 3 绑定(bind)到集合中的当前项

标签 wpf silverlight binding silverlight-4.0 silverlight-3.0

绑定(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/

相关文章:

.net - 如何绑定(bind)到列表中的最后一项?

c# - 在 C# StreamGeometry Xaml 标记中创建非描边区域

c# - 在 Silverlight 中将十六进制值转换为 SolidColorBrush

android - Textview 和 Seekbar 绑定(bind)到同一个属性,但是 textview 不显示更新的值

c# - 在 ListView 中禁用/更改 HoverOver 的颜色

c# - 页眉和页脚布局

c# - WCF RIA 服务——加载所有数据后采取行动

c++ - 致命的Python错误: Couldn't create autoTLSkey mapping with C++ binding

wpf - 使用分组绑定(bind) WPF 中的列表框

c# - 更改 DataGrid(WPF) 行中的单元格正在更改下方行中的单元格