开发者

Silverlight 3 Binding to the Current Item in a Collection

The Binding syntax, {Binding /}, works in WPF but does not work at all in Silverlight 3:

<ContentControl Content="{Binding MyCollection}">
    <ContentControl.ContentTempla开发者_如何学编程te>
        <DataTemplate>
            <ContentControl Content="{Binding /}" />
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>

What's the way to approach this in Silverlight?


When binding to a collection in WPF, you're actually binding to something that understands the concept of "Current Item".

However in Silverlight what you're binding to doesn't have this concept. So you need to do it yourself.

For example in a MVVM app expose a property.

<ListBox SelectedItem="{Binding MyCurrentItem}" 
         ItemsSource="{Binding MyCollection}"/>
<ContentControl Content="{Binding MyCurrentItem}" />

or do some element binding

<ListBox x:Name="listBox" 
         ItemsSource="{Binding MyCollection}"/>
<ContentControl Content="{Binding SelectedItem, ElementName=listBox}" />


I think you want {Binding} or {Binding .}, either of which do the same thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜