none
toolkit Context Menu getting selected item within a long list selector?

    Question

  • Hi

    I have a longlist selector in whichi am trying to use a contex menu in the on click of my contex menu i am trying to get the item that has been tapped i have been able todo this with a regular listbox however there are some properties not available on the longlist selector can someone help with this?

    here is my xaml and code behind.

    <DataTemplate x:Key="albumgroupItemHeader">  
                <Border Background="{StaticResource PhoneAccentBrush}" Margin="6" Height="70" Width="70" HorizontalAlignment="Left" VerticalAlignment="Top">  
                    <TextBlock Text="{Binding Title}" Style="{StaticResource PhoneTextExtraLargeStyle}" VerticalAlignment="Bottom" HorizontalAlignment="Left" />  
                </Border>  
            </DataTemplate>  
     
            <DataTemplate x:Key="albumgroupHeader">  
                <Border Background="{StaticResource PhoneAccentBrush}" Margin="6" Height="90" Width="90" HorizontalAlignment="Left" VerticalAlignment="Top">  
                    <TextBlock Text="{Binding Title}" Style="{StaticResource PhoneTextExtraLargeStyle}" VerticalAlignment="Bottom" HorizontalAlignment="Left" />  
                </Border>  
            </DataTemplate>  
     
            <!-- The template for movie items -->  
            <DataTemplate x:Key="albumItemTemplate">  
                <StackPanel Orientation="Horizontal">  
                    <StackPanel Margin="5,0,0,15">  
                        <Image Width="173" Height="173" Source="{Binding AlbumImage}" Tap="Image_Tap"></Image>  
                        <Image Source="/SkyMusic;component/Images/appbar.transport.playt.png" Width="44" Height="44" Margin="0,-168,5,0" VerticalAlignment="Top" HorizontalAlignment="Right"></Image>  
                    </StackPanel>  
                    <TextBlock TextWrapping="Wrap" FontFamily="Segoe WP" Text ="{Binding AlbumTitle}" FontSize="36" Margin="5,0,0,0" Width="300" Tap="TextBlock_Tap">  
                        <toolkit:ContextMenuService.ContextMenu>  
                                <toolkit:ContextMenu>  
                                    <toolkit:MenuItem Header="add to now playing" />  
                                    <toolkit:MenuItem Header="pin to start" />  
                                </toolkit:ContextMenu>  
                            </toolkit:ContextMenuService.ContextMenu>  
                    </TextBlock>  
                </StackPanel>  
            </DataTemplate>  
        </navigation:PhoneApplicationPage.Resources>  
     
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,150,12,0">  
                <toolkit:LongListSelector x:Name="lbalbums" Background="Transparent" ItemsSource="{Binding albumlists}" 
                        GroupHeaderTemplate="{StaticResource albumgroupItemHeader}" 
                        GroupItemTemplate="{StaticResource albumgroupHeader}" 
                        ItemTemplate="{StaticResource albumItemTemplate}" >  
                    <toolkit:LongListSelector.GroupItemsPanel>  
                        <ItemsPanelTemplate>  
                            <toolkit:WrapPanel Orientation="Horizontal"/>  
                        </ItemsPanelTemplate>  
                    </toolkit:LongListSelector.GroupItemsPanel>  
                </toolkit:LongListSelector>  
            </Grid>  
     
    code behind  
     

    private

     

     

    void PinAlbum_Click(object sender, RoutedEventArgs e)

     

    {

    string header = (sender as MenuItem).Header.ToString();  
                ListBoxItem selectedListBoxItem = this.lbalbums.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;  
                if (selectedListBoxItem == null)  
                {  
                    return;  
                }  
     
                ListBox view = ItemsControl.ItemsControlFromItemContainer(selectedListBoxItem) as ListBox;  
                int index = view.ItemContainerGenerator.IndexFromContainer(selectedListBoxItem);  
                if (index == -1)  
                {  
                    return;  
                }  
    Wednesday, February 08, 2012 3:32 AM

Answers

  • In a databound ListBox or LongListSelector, the DataContext property of each item is the object that it is databound to. If you put a ContextMenu in a list item, it will inherit the item's DataContext property.

    Assuming that the Click handler for the "pin to start" MenuItem is like this:
    <toolkit:MenuItem Header="pin to start" Click="PinAlbum_Click"/> 

    The Click handler would look something like this:
            private void PinAlbum_Click(object sender, RoutedEventArgs e) 
            { 
                Album selectedAlbum = (sender as MenuItem).DataContext as Album; 
            } 

    This is assuming that you are databinding your LongListSelector to a list of "Album" objects (replace with the class you are actually using). sender is the MenuItem that was clicked. Access its DataContext, which will be the selected item.


    Richard Woo
    Wednesday, February 08, 2012 9:02 AM

All replies

  • In a databound ListBox or LongListSelector, the DataContext property of each item is the object that it is databound to. If you put a ContextMenu in a list item, it will inherit the item's DataContext property.

    Assuming that the Click handler for the "pin to start" MenuItem is like this:
    <toolkit:MenuItem Header="pin to start" Click="PinAlbum_Click"/> 

    The Click handler would look something like this:
            private void PinAlbum_Click(object sender, RoutedEventArgs e) 
            { 
                Album selectedAlbum = (sender as MenuItem).DataContext as Album; 
            } 

    This is assuming that you are databinding your LongListSelector to a list of "Album" objects (replace with the class you are actually using). sender is the MenuItem that was clicked. Access its DataContext, which will be the selected item.


    Richard Woo
    Wednesday, February 08, 2012 9:02 AM
  • It returns null in my case. Is there any other way?


    Wednesday, December 26, 2012 5:22 AM
  • It returns null in my case. Is there any other way?

    It's hard to guess what might be wrong if you don't show your code.

    If you're talking about the line with DataContext, I would think that you are casting DataContext to the wrong type of object. It should be the class of your view model item. Put a breakpoint there and see what type of object it is.


    Wednesday, December 26, 2012 8:54 AM
  • Hi,

    Thanks for the reply. I was placing the context menu in wrong place. Got it working now.

    Thursday, December 27, 2012 6:35 AM
  • Thanks buddy you help me a lot!
    Monday, August 12, 2013 6:20 AM