获取所选项的父ObservableCollection - WP8

时间:2014-09-22 04:55:23

标签: c# xaml windows-phone-8 longlistselector

我正在编写应用程序来播放流媒体音乐,在应用程序中我有很多列表(排名列表,搜索结果列表,突出显示歌曲列表......),每个列表都有一个相同的数据模板我绑定到每个页面的LongListSelector。所以我使用这个datatemplate作为资源并将其放在app.xaml

<DataTemplate x:Key="BasicVideoTemplate">
    <Grid Tap="ChangeSong_Tap" RowsAuto="50,50" ColumnsAuto="150,*" Background="White" Margin="5,0,5,10">
        <Grid.ColumnDefinition> 
            <ColumnDefinition Width = "150"/>
            <ColumnDefinition Width = "*"/>
        </Grid.ColumnDefinition>
        <Grid.RowDefinition> 
             <RowDefinition Height = "50"/>
             <RowDefinition Height = "50"/>
        </Grid.RowDefinition>
        <Border BorderThickness="1" BorderBrush="Black" Grid.RowSpan="2" Grid.Column="0" VerticalAlignment="Center" Margin="5,0,5,0">
            <Image Source="{Binding Cover}"/>
        </Border>
        <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1" Style="{StaticResource BlackTextBlock}" Margin="5,0,0,0"/>
        <TextBlock Text="{Binding Artist}" Grid.Row="1" Grid.Column="1" Foreground="Black" Margin="5,0,0,0"/>
        <!-- .............. -->
    </Grid>
</DataTemplate>

这段代码(我放在app.xaml.cs中)从列表中选择一首歌曲,从这个项目创建一个AudioTrack并导航到playSongPage:

private void ChangeSong_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    var item = (SongItemModel)(sender as FrameworkElement).DataContext;
    App.Model.ChangeSong(item.Id);   /// this code will create a audio track for this item
    (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Pages/DetailSongPage.xaml", UriKind.Relative));
}

问题在于,我必须创建一个List&lt; AudioTrack&GT;对于我的播放列表,那么如何获取所点击项目的父列表并将其添加到列表&lt; AudioTrack&GT; ,所有这些代码都放在app.xaml.cs ???

1 个答案:

答案 0 :(得分:1)

我会在每个SelectionChanged的{​​{1}}事件中处理它。网格上的整个Tap事物并不适合我。

longlistselector

<phone:LongListSelector x:Name="myLSS" SelectionChanged="myLSS_SelectionChanged"/>