开发者

List binding in silverlight 4 (c#) with mvvm

I have a list box bound to a collection, PlaylistTracks. PlaylistTracks contains a list id and a track id. All tracks are stored in another collection, Tracks. This collection has the track name.

Can I get the list box to display the track name from the collection its not bound to? Something like this:

<ListBox Name="PlayListTracksBox" 
ItemsSource="{Binding Path=PlaylistTracks, Mode=TwoWay}" 
DisplayMemberPath="Tracks.Name" 
SelectedItem="{Binding Path=SelectedListTrack开发者_JS百科, Mode=TwoWay}"></ListBox>


I believe overriding ToString() for whatever class PlaylistTracks contains would be the fastest way to get what you want to work. Inside of ToString you'd have to put whatever logic you need to look up the correct track, and return the name therefrom.

If you can get the track name into the top-level object in your ItemsSource, then the more "Silverlighty" way to get this to work is to provide an ItemTemplate and bind to whatever you want. Something like:

    <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Tracks.Name}"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜