0

我有以下 view.xaml 并将集合(来自 viewmodel 的 SavedTracksCollection)绑定到此列表框,并在 UI 中显示项目。

   <phone:PanoramaItem  Name="MusicTracks"  Header="Saved Tracks" >
            <Grid>
                <ListBox x:Name="list" ItemsSource="{Binding SavedTracksCollection}" SelectedItem="{Binding SelectedItemTrack,Mode=TwoWay}">                     

                    <ListBox.ItemTemplate>    
                            <DataTemplate>
                            <Button  Background="Red"  >                                
                                <StackPanel Orientation="Vertical">                                            
                                       <TextBlock Text="{Binding TrackTitle}"/>
                                        <TextBlock Text="{Binding TrackUri}"/>
                                 </StackPanel> 

                            </Button>
                           <DataTemplate>
                   </ListBox.ItemTemplate>  
           </Grid>
 </phone:PanoramaItem>

我在我的视图模型中定义了以下属性(此视图模型设置为我的视图的数据上下文),用于选定项绑定“SelectedItemTrack”。我将 SavedTracksCollection 绑定到列表的项目源。

  private SavedTracksModel _SelectedItemTrack;
    public SavedTracksModel SelectedItemTrack
    {
        get {
            return _SelectedItemTrack;
        }
        set
        {
            if (value!=null)              
            _SelectedItemTrack = value;
            //RaisePropertyChanged("SelectedItemTrack"); I dont think we need this.Let me know otherwise.


        }
    }

  private List<SavedTracksModel> _SavedTracksCollection = new List<SavedTracksModel>();
    public List<SavedTracksModel> SavedTracksCollection
    {
        get
        {
            return GetSavedTracks();
        }
        set
        {
            this._SavedTracksCollection = value;
            RaisePropertyChanged("SavedTracksCollection");
        }
    }

但是我无法确定当用户从列表框中选择一个项目时如何捕获 SelectedITem 事件。目前它不会触发 SelectedITemTrack 的 set 方法。一旦我使用所选项目绑定的详细信息捕获事件“TrackUri” “我想转到一个可以播放曲目的新页面。

知道如何解决这个问题吗?

4

1 回答 1

1

我能想到的第一个解决方案,为什么不在ListBox 上使用 SelectionChanged 事件

<ListBox x:Name="list" ItemsSource="{Binding SavedTracksCollection}"
    SelectedItem="{Binding SelectedItemTrack,Mode=TwoWay}"
    SelectionChanged="List_OnSelectionChanged"/>

// in code behind
private void List_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // navigate here after validating the selected item  
    // or raise Command in your ViewModel programatically  
}
于 2013-11-07T22:14:54.320 回答