krrishna
krrishna

Reputation: 2078

detect selecteditem details in ListBox from MVVM app in windows phone

I have the following view.xaml and I bind a collection(SavedTracksCollection from viewmodel) to this list box and it displays the items in 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>

And the i have the following property defined in my viewmodel(this viewmodel is set as data context for my view) for the selecteditem binding "SelectedItemTrack".And i am binding SavedTracksCollection to the itemsource of the list.

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

But i am not able to determine how do i capture the SelectedITem event when user selectes an item from the Listbox .Currently it doesn't trigger the set method of the SelectedITemTrack .Once i capture the event with the details of selected item binding "TrackUri" i want to go to a new page where i can play the track.

any idea how to fix the issue ?

Upvotes: 0

Views: 576

Answers (1)

Martin Suchan
Martin Suchan

Reputation: 10620

The first solution I can think of, why not just use the SelectionChanged event on ListBox?

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

Upvotes: 1

Related Questions