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

Context Menu with ListBox

6 Answers 102 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Naren
Top achievements
Rank 1
Naren asked on 27 Sep 2010, 04:18 PM
Hi,
I am using ContextMenu.on ListBox.

My Xaml is as follows:




 <Grid x:Name="LayoutRoot" Background="White">
        <ListBox x:Name="whatfirstAttributeList" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled" dragDrop:RadDragAndDropManager.AllowDrag="True" FontWeight="Normal" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="300" Background="{Binding Name, Converter={StaticResource myconverter}}">
                        <TextBlock Text="{Binding Name}" x:Name="AttributeList" FontSize="12"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <telerikNav:RadContextMenu.ContextMenu>
                <telerikNav:RadContextMenu x:Name="ContextMenu" ItemClick="ContextMenu_ItemClick" Opened="ContextMenu_Opened">
                    <telerikNav:RadMenuItem Header="Add Attribute" />
                    <telerikNav:RadMenuItem Header="Delete Attribute"/>
                </telerikNav:RadContextMenu>
            </telerikNav:RadContextMenu.ContextMenu>
        </ListBox>
    </Grid>



How can I get selected Item on ListBox in following cases?
1>
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            if (this.whatfirstAttributeList.SelectedItem != null)
            {
this always returns Null.........
2>
private void ContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            DataItem dataItem = this.selectedColumnItem.DataContext as DataItem;
This doesn't work....always give error....

6 Answers, 1 is accepted

Sort by
0
Zarko
Telerik team
answered on 28 Sep 2010, 01:06 PM
Hello Naren,

 Your SelectedItem is null because to actually select it you have to left click it. You can get the right clicked item with GetClickedElement method of the RadContextMenu. Please examine the attached project and if you have any further questions feel free to ask.

Best wishes,
Zarko
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Naren
Top achievements
Rank 1
answered on 29 Sep 2010, 08:17 AM
 
Hi Zarko,
Thanx for your reply......

but its not working in my case
I have following statement

System.Windows.Controls.ListBoxItem selected = ContextMenu.GetClickedElement<System.Windows.Controls.ListBoxItem>();

where i get following error
"Value does not fall within the expected range.".....

what may be the problem???
Thanx in advance
0
Naren
Top achievements
Rank 1
answered on 29 Sep 2010, 11:19 AM
Hi Zarko,
 I tried GetClickedElement method of the RadContextMenu It only works on UserControl and not on child control.

In my scenario I have ListBox and Context Menu on ChildWindow. And GetClickedElement method is not working on Child Window..

can you please help..
0
George
Telerik team
answered on 01 Oct 2010, 03:36 PM
Hello Naren,

We are aware of such problem with RadContextMenu. In order to get the clicked element in a ChildWindow, I would suggest you to register the RadContextMenu with the ListBox in the ChildWindow

Attached you can find the sample. I hope this helps.
 

Regards,
George
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Software Support
Top achievements
Rank 1
answered on 06 Apr 2011, 10:33 PM
i got the following error when i used context menu in child control. I am using v.2011.1.315.1040.
"Value does not fall within the expected range.".....

My Xaml is as follows:

<controls:ChildWindow xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  x:Class="contextMenuDemo.ChildWindow1"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="400" Height="300" 
           Title="ChildWindow1">
    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
<TextBox Text="aaaa" Width="50" Height="30" >
<telerik:RadContextMenu.ContextMenu>
<telerik:RadContextMenu>
<telerik:RadMenuItem Header="copy" Click="RadMenuItem_Click"></telerik:RadMenuItem>
<telerik:RadMenuItem Header="paste" Click="RadMenuItem_Click"></telerik:RadMenuItem>
</telerik:RadContextMenu>
</telerik:RadContextMenu.ContextMenu>
</TextBox>
    </Grid>
</controls:ChildWindow>

can you please help on this quickly..
0
Hristo
Telerik team
answered on 08 Apr 2011, 10:14 AM
Hello Bradford,

Please upgrade to the latest internal build in order to resolve this problem.

Best wishes,
Hristo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Naren
Top achievements
Rank 1
Answers by
Zarko
Telerik team
Naren
Top achievements
Rank 1
George
Telerik team
Software Support
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or