RadContextMenu selection

6 posts, 1 answers
  1. Serrin
    Serrin avatar
    286 posts
    Member since:
    Sep 2008

    Posted 16 Jan 2009 Link to this post

    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!!

  2. Answer
    Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 19 Jan 2009 Link to this post

    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.
  3. DevCraft banner
  4. Serrin
    Serrin avatar
    286 posts
    Member since:
    Sep 2008

    Posted 19 Jan 2009 Link to this post

    Brilliant! :) 

    I think I'm going to have to credit you in my About page.
  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 19 Jan 2009 Link to this post

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

    Posted 26 Dec 2012 Link to this post

    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
  7. Pana
    Admin
    Pana avatar
    748 posts

    Posted 28 Dec 2012 Link to this post

    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.

Back to Top
DevCraft banner