【问题标题】:Get the parent ObservableCollection which Item selected - WP8获取选择了哪个项目的父 ObservableCollection - WP8
【发布时间】:2014-09-22 04:55:22
【问题描述】:

我正在编写一个应用程序来播放流媒体音乐,在应用程序中我有很多列表(排名列表、搜索结果列表、突出显示歌曲列表.....),每个列表都有一个相同的数据模板,我绑定到 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,那么我怎样才能获得点击项目的父列表并将其添加到 List 中,而所有这些代码都放在应用程序中。 xaml.cs ???

【问题讨论】:

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


    【解决方案1】:

    我会在每个longlistselectorSelectionChanged 事件中处理它。网格上的整个 Tap 东西对我来说并不合适。

    <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));                 
    }
    

    【讨论】:

    • 那么 selectionChanged 会将整个列表作为 itemoures 吗?非常感谢:D
    • 是的,longlistselector 是引发事件的人。这样您就可以从发件人那里获取它。
    • 寻找这么简单的东西太久了 - thnx
    猜你喜欢
    • 2012-10-29
    • 1970-01-01
    • 2015-10-20
    • 2011-11-26
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    相关资源
    最近更新 更多