This is a migrated thread and some comments may be shown as answers.

How to get selected row?

4 Answers 81 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Froggy
Top achievements
Rank 2
Froggy asked on 08 May 2012, 03:44 PM
Hi,

How is is possible to get the selected row from which the context menu was called.  Specifically I want to retrieve my databound object.  Something like:

            ViewEntry data = (sender as ListBox).SelectedItem as ViewEntry;

This works in the listbox.  But how does this work with the context menu.

Its documented to use RadDataBoundListBoxItem but i don't see how to gain access directly to my object.

thanks!

4 Answers, 1 is accepted

Sort by
0
Accepted
Tim
Top achievements
Rank 1
answered on 08 May 2012, 07:23 PM
Hi Steven,

If you aren't using a Commands, it is a 2-step process. 

First, add your markup for your context menu inside of your databound control, as seen in the markup from this page: http://www.telerik.com/help/windows-phone/radcontextmenu-howto-dynamic.html.

In my case, I did the following. 2 events to note: 'Opening' on the context menu, and 'Tap' on the ContextMenuItem. 

<telerikData:RadJumpList>
    <telerikPrimitives:RadContextMenu.ContextMenu>
        <telerikPrimitives:RadContextMenu Opening="RadContextMenu_Opening" OpenGesture="Hold">
            <telerikPrimitives:RadContextMenuItem Content="delete" Tap="RadContextMenuItem_Tap" />
        </telerikPrimitives:RadContextMenu>
    </telerikPrimitives:RadContextMenu.ContextMenu>
</telerikData:RadJumpList>

To implement this, first add a variable in your code to store the selected item:
private ViewEntry selectedViewEntry;

In the opening event of your menu, you can get the context of the item that was selected. You'll have to store it in a variable to be referenced in the next step:
private void RadContextMenu_Opening(object sender, ContextMenuOpeningEventArgs e)
{
    RadDataBoundListBoxItem item = e.FocusedElement as RadDataBoundListBoxItem;
    this.selectedViewEntry = item.DataContext as ViewEntry;          
}

Then in the ContextMenuItem tap event:

private void RadContextMenuItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    //do something with your selected ViewEntry
}


0
Froggy
Top achievements
Rank 2
answered on 09 May 2012, 02:29 PM
Hi,

Thanks very much!!!

I made 2 mistakes.

I did not figure this part out:

this.selectedViewEntry = item.DataContext as ViewEntry;   


But worse I was using the SliverLight <ListBox> I forgot to switch the component so for anyone else, this is how the XML looks

                <telerikDataControls:RadJumpList Margin="0,0,-12,0" ItemsSource="{Binding Items}" Name="listOverview" SelectionChanged="listOverview_SelectionChanged_1">
                    <telerikDataControls:RadJumpList.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="0,0,0,17">
                                <Rectangle HorizontalAlignment="Left" VerticalAlignment="Top" Height="100" Width="100" Fill="#FFE5001b" Margin="12,0,0,0"/>
                                <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Height="100" Width="100" Text="{Binding PainValue}" TextWrapping="NoWrap" Margin="12,12,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}" TextAlignment="Center"/>
                                <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="100" Style="{StaticResource PhoneTextSubtleStyle}"
                                           Text="Pain" Name="txtPain" TextWrapping="NoWrap" Margin="12,68,0,0" TextAlignment="Center"/>
                                <StackPanel VerticalAlignment="Top" Height="100" HorizontalAlignment="Left" Margin="117,10,0,0" Name="stackPanel1" Width="400">
                                    <TextBlock Text="{Binding StartDisplayDate}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                    <TextBlock Margin="12,-6,12,0" Text="{Binding TypeOfHeadacheString}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                        </telerikDataControls:RadJumpList.ItemTemplate>
                    <telerikPrimitives:RadContextMenu.ContextMenu>
                        <telerikPrimitives:RadContextMenu 
                                        IsFadeEnabled="True" Opening="OnMenuOpening" IsZoomEnabled="True">
                            <telerikPrimitives:RadContextMenuItem Content="{Binding Path=LocalizedResources.edit, Source={StaticResource LocalizedStrings}}" 
                                                  Tapped="OnChangeEditTapped"/>
                            <telerikPrimitives:RadContextMenuItem Content="{Binding Path=LocalizedResources.delete, Source={StaticResource LocalizedStrings}}"
                                                  Tapped="OnChangeDeleteTapped" />
                        </telerikPrimitives:RadContextMenu>
                    </telerikPrimitives:RadContextMenu.ContextMenu>
                </telerikDataControls:RadJumpList>


0
Tim
Top achievements
Rank 1
answered on 09 May 2012, 03:23 PM
I don't know if there would be any conflict between the two controls, but it could probably work with the regular <Listbox> control. In your context menu opening event, you'll cast it to a ListBoxItem rather than a RadDataBoundListBoxItem
0
Michael
Top achievements
Rank 1
answered on 08 Jul 2012, 04:30 PM
Any idea how to do this WITH command?  I can get to the DataBoundListBox control, but I need the CommandParameter to be the object bound to the row that the context menu was opened on.  Thanks!

Update:
Nevermind, I got it.  I put the ContextMenu right inside the main element of my DataTemplate.  Hopefully that doesn't have some bad side effect, like getting too many ContextMenus created.  Seems to work though.
<telerikPrimitives:RadContextMenu.ContextMenu>
    <telerikPrimitives:RadContextMenu IsFadeEnabled="False" IsZoomEnabled="False">
        <telerikPrimitives:RadContextMenuItem x:Name="NavigateToDefinitionMenuItem"
                                                  Content="Navigate to detail"
                                                  Command="{Binding Path=DataContext.ShowDetailCommand, ElementName=RootElement}"
                                                  CommandParameter="{Binding}" />
    </telerikPrimitives:RadContextMenu>
</telerikPrimitives:RadContextMenu.ContextMenu>
Tags
ContextMenu
Asked by
Froggy
Top achievements
Rank 2
Answers by
Tim
Top achievements
Rank 1
Froggy
Top achievements
Rank 2
Michael
Top achievements
Rank 1
Share this question
or