How to get selected row?

5 posts, 1 answers
  1. Froggy
    Froggy avatar
    16 posts
    Member since:
    Apr 2012

    Posted 08 May 2012 Link to this post

    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!

  2. Answer
    Tim
    Tim avatar
    19 posts
    Member since:
    Dec 2011

    Posted 08 May 2012 Link to this post

    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
    }


  3. DevCraft banner
  4. Froggy
    Froggy avatar
    16 posts
    Member since:
    Apr 2012

    Posted 09 May 2012 Link to this post

    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>


  5. Tim
    Tim avatar
    19 posts
    Member since:
    Dec 2011

    Posted 09 May 2012 Link to this post

    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
  6. Michael
    Michael avatar
    52 posts
    Member since:
    Dec 2005

    Posted 08 Jul 2012 Link to this post

    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>
Back to Top
DevCraft banner