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

RadContextMenu selection

5 Answers 115 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Serrin
Top achievements
Rank 1
Serrin asked on 16 Jan 2009, 03:16 PM
Hi,

This may sound like a dumb question, but I can't find it in the documentation or the demos...  I have a context menu which displays on the MouseLeftButtonDown event of the main layout grid for a usercontrol, which displays fine.  The question is, how do I handle a selection from the context menu?  If I try to add an event handler (Click, MouseLeftButtonUp, etc.), I get XamlReader.Load() does not accept event handlers [Line 297, Position 70].  Here is a simplified version of the code I'm using, basically I use this usercontrol as a container to load other items, but the entire container needs to respond to the mouse event for the context menu:

<UserControl xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"  x:Class="Silverlight_Portfolio.GalleryItem" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    Width="150" Height="150">  
      
    <Grid x:Name="LayoutRoot" Background="Transparent" ShowGridLines="False">          
        <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="10" /> 
            <ColumnDefinition Width="130" /> 
            <ColumnDefinition Width="10" /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
            <RowDefinition Height="10" /> 
            <RowDefinition Height="110" /> 
            <RowDefinition Height="30" /> 
        </Grid.RowDefinitions> 
        <telerikNavigation:RadContextMenu.ContextMenu> 
            <telerikNavigation:RadContextMenu x:Name="RadContext" Placement="Absolute" EventName="MouseLeftButtonDown">  
                <telerikNavigation:RadMenuItem Header="View this picture" Tag="ViewThis" /> 
                <telerikNavigation:RadMenuItem Header="More by this artist" Tag="MoreByArtist" /> 
                <telerikNavigation:RadMenuItem Header="More from this genre" Tag="MoreByGenre" /> 
                <telerikNavigation:RadMenuItem Header="Add To favorites" Tag="Favorite" /> 
            </telerikNavigation:RadContextMenu> 
        </telerikNavigation:RadContextMenu.ContextMenu> 
        <Border x:Name="styleBorder" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" Grid.RowSpan="3" Margin="2 4 2 4">  
        </Border> 
</Grid> 
</UserControl> 

 

In a perfect world, when I select the second item, "More by this artist", it would grab a value stored in the Tag of the styleBorder and use that in the event handling the selection/click of the context menu item, pulling up more pictures by the artist.  So... how would I go about doing handling this event to make this happen? :)

Thanks!!

5 Answers, 1 is accepted

Sort by
0
Accepted
Hristo
Telerik team
answered on 19 Jan 2009, 01:49 PM
Hi Serrin,

Almost all of our controls are using routed events. RadMenuItem expose Click, Check, Unchecked, SubmenuOpened and SubmenuClosed routed events. This means that this events bubbles up in the visual tree and you can handle them where you want.

First you need to add using Telerik.Windows namespace.
Then in code you can add this code to handle RadMenuItem.Click event:

public Page25()  
{  
    InitializeComponent();  
    this.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));  
}  
 
private void OnMenuItemClick(object sender, RoutedEventArgs e)  
{  
    RadRoutedEventArgs args = e as RadRoutedEventArgs;  
    RadMenuItem menuItem = args.OriginalSource as RadMenuItem;  
    string tag = Convert.ToString(menuItem.Tag);  
    switch (tag)  
    {  
        case "ViewThis":  
            // ViewThis();  
            break;  
        case "MoreByArtist":  
            // MoreByArtist();  
            break;  
    }             

Let me know if you need more help.

Kind regards,
Hristo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Serrin
Top achievements
Rank 1
answered on 19 Jan 2009, 03:00 PM
Brilliant! :) 

I think I'm going to have to credit you in my About page.
0
Hristo
Telerik team
answered on 19 Jan 2009, 04:06 PM
Hello Serrin,

I'm glad I could help.

All the best,
Hristo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Sean
Top achievements
Rank 1
answered on 26 Dec 2012, 07:14 PM
How do I prevent the event from bubbling? Once the submenu item has been identified I want to stop the event bubble to the root menu's itemclick
0
Pana
Telerik team
answered on 28 Dec 2012, 07:33 AM
Hi,

RoutedEvents should not be stopped from propagation but rather "Handled". Just add:
e.Handled = true;

In the RadMenuItem's Click handler and the RadMenu's ItemClick will not be fired.

If the above code does not help you please provide a more detailed information about your scenario so we could provide adequate assistance.

Greetings,
Pana
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
Menu
Asked by
Serrin
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Serrin
Top achievements
Rank 1
Sean
Top achievements
Rank 1
Pana
Telerik team
Share this question
or