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

Selected Item doesn´t work on ContextMenu

3 Answers 118 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.
Thiago
Top achievements
Rank 1
Thiago asked on 23 Jan 2013, 11:27 AM
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

3 Answers, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 24 Jan 2013, 09:37 AM
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.
0
Thiago
Top achievements
Rank 1
answered on 24 Jan 2013, 11:39 AM
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!






0
Victor
Telerik team
answered on 25 Jan 2013, 09:40 AM
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.
Tags
ContextMenu
Asked by
Thiago
Top achievements
Rank 1
Answers by
Victor
Telerik team
Thiago
Top achievements
Rank 1
Share this question
or