Selected Item doesn´t work on ContextMenu

4 posts, 0 answers
  1. Thiago
    Thiago avatar
    16 posts
    Member since:
    May 2012

    Posted 23 Jan 2013 Link to this post

    Hello,
    I´m trying to get the selected item of a databound Silverlight ListBox. My databinding is working very well, all the data are appearing on the screen. The problem is to retrieve the selected item through RadContextMenu. In the Opening event, this is the code:
    var selectedItem;
     
    ListBoxItem item = e.FocusedElement as ListBoxItem;
    selectedItem = item.DataContext as MyObjectModel;
    But the "item" variable ever returns null. I´ve already checked my data source and it´s all ok, all the data is being returned. I´ve also changed to RadDataBoundListBox and the behavior is the same.
    Here is my xaml code under a ListBox silverlight:

    <ListBox ItemsSource="{Binding LstAlimentos}"
                    Margin="0,20,0,0"
                    Name="ltbAlimentos">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="50" />
                                        <RowDefinition Height="50" />
                                        <RowDefinition Height="50" />
                                    </Grid.RowDefinitions>
     
                                    <TextBlock Width="600"
                                               Height="50"
                                               HorizontalAlignment="Left"
                                               TextAlignment="Left"
                                               Margin="0,0,0,0"
                                               Text="{Binding Nome}"
                                               FontFamily="Segoe WP"
                                               FontWeight="Bold"
                                               FontSize="25"
                                               Grid.Row="0"
                                               Foreground="{Binding Favorito, Converter={StaticResource BoolToBrushConverter}}"/>
     
                                    <TextBlock Width="300"
                                               Height="50"
                                               HorizontalAlignment="Left"
                                               TextAlignment="Left"
                                               Margin="0,-30,0,0"
                                               Text="{Binding Quantidade}"
                                               FontFamily="{StaticResource PhoneFontFamilyLight}"
                                               FontSize="25"
                                               Grid.Column="1"
                                               Grid.Row="1"
                                               Foreground="{Binding Favorito, Converter={StaticResource BoolToBrushConverter}}"/>
     
                                    <TextBlock Width="160"
                                               Height="50"
                                               HorizontalAlignment="Left"
                                               TextAlignment="Left"
                                               Margin="-8,-50,0,0"
                                               Text="{Binding Pontos, StringFormat=' {0} pontos'}"
                                               FontFamily="{StaticResource PhoneFontFamilyLight}"
                                               FontSize="25"
                                               Grid.Column="2"
                                               Grid.Row="2"
                                               Foreground="{Binding Favorito, Converter={StaticResource BoolToBrushConverter}}" />
                                     
                                    <telerikPrimitives:RadContextMenu.ContextMenu>
                                        <telerikPrimitives:RadContextMenu OpenGesture="Hold"
                                                                          x:Name="ctmOpcoes"
                                                                          Opening="ctmOpcoes_Opening">
                                            <telerikPrimitives:RadContextMenuItem Content="adicionar"
                                                                                  Name="mnuAdicionar"
                                                                                  Tap="mnuAdicionar_Tap"
                                                                                  Visibility="{Binding Favorito, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter='Adicionar'}"/>
                                            <telerikPrimitives:RadContextMenuItem Content="excluir"
                                                                                  Name="mnuExcluir"
                                                                                  Tap="mnuExcluir_Tap"
                                                                                  Visibility="{Binding Favorito, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter='Excluir'}" />
                                        </telerikPrimitives:RadContextMenu>
                                    </telerikPrimitives:RadContextMenu.ContextMenu>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

    Am I doing something wrong and the selection doesn´t works?

    Thanks in advance,

    Thiago
  2. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 24 Jan 2013 Link to this post

    Hello Thiago,

    Thanks for writing. Did you set the FocusedElementType attached property like described here (The FocusedElement property section)?

    All the best,
    Victor
    the Telerik team
    Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
  3. DevCraft banner
  4. Thiago
    Thiago avatar
    16 posts
    Member since:
    May 2012

    Posted 24 Jan 2013 Link to this post

    Hello Victor,
    Thank you by the feedback!
    I hadn´t set this attached property, but now I´ve changed and still doesn´t works.
    In debug mode, I´ve noticed the return type from e.FocusedElement of ContextMenuOpeningEventArgs.
    They are described on the attached images. The first is from my project and the other from sample provided by the article.
    I´m supposing that the layout is the problem. In the sample described on the article works sucessfully!






  5. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 25 Jan 2013 Link to this post

    Hi Thiago,

    I am afraid that from the images and the XAML alone I am not able to determine why your does not work as expected. If you post a support ticket and attach your app I will able to help you further.

    Kind regards,
    Victor
    the Telerik team
    Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
Back to Top