Search code examples
c#xamlwindows-phone-8longlistselector

Get the parent ObservableCollection which Item selected - WP8


I'm writing an app to play streaming music, In app I have many list (Ranking list, Search Result List, Highlight song list .....), Each list have a same datatemplate which I bind to a LongListSelector for each Page. So I use this datatemplate as resources and put it in 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>

And this code (which i put in app.xaml.cs) to select a song from list, create a AudioTrack from this item and navigate to 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));
}

Problem here is, I have to create a List< AudioTrack> for my playlist , So how can I get the parent list of clicked item and add it to the List< AudioTrack> , while all this code was put in app.xaml.cs ???


Solution

  • I would handle it in the SelectionChanged event of each longlistselector instead. The whole Tap thing on the grid doesn't sit well with me.

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

    // event handler changes to
    private void myLSS_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LongListSelector lls = sender as LongListSelector;  // get lls
        var item = (SongItemModel) lls.SelectedItem;
        App.Model.ChangeSong(item.Id);   /// this code will create a audio track for this item
    
    
        // now your ObservableCollection is just the ItemsSource, save a reference to it
        // in the State manager so you can reference it on another page if you wish
        ObservableCollection<SongItemModel> obs = (ObservableCollection<SongItemModel>) lls.ItemsSource;
        PhoneApplicationService.Current.State["current_obs"] = obs;
    
        // navigate..............
        (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Pages/DetailSongPage.xaml", UriKind.Relative));                 
    }