Context Menu with ListBox

7 posts, 0 answers
  1. Naren
    Naren avatar
    53 posts
    Member since:
    Aug 2010

    Posted 27 Sep 2010 Link to this post

    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....
  2. Zarko
    Admin
    Zarko avatar
    755 posts

    Posted 28 Sep 2010 Link to this post

    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
  3. Naren
    Naren avatar
    53 posts
    Member since:
    Aug 2010

    Posted 29 Sep 2010 Link to this post

     
    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
  4. Naren
    Naren avatar
    53 posts
    Member since:
    Aug 2010

    Posted 29 Sep 2010 Link to this post

    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..
  5. George
    Admin
    George avatar
    1332 posts

    Posted 01 Oct 2010 Link to this post

    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
  6. Software Support
    Software Support avatar
    7 posts
    Member since:
    May 2008

    Posted 06 Apr 2011 Link to this post

    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..
  7. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 08 Apr 2011 Link to this post

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