MenuItem_Click Event for Custom Menu

5 posts, 1 answers
  1. Yas
    Yas avatar
    10 posts
    Member since:
    Jul 2009

    Posted 09 May 2011 Link to this post

    Hello,

    I have implemented a custom Menu similar to the one shown in the demos [http://demos.telerik.com/wpf/Telerik.Windows.Examples.xbap] using WPF. I am using the "Integration Example" which combines an Image, a Title and a Summary. The problem I am facing is that I can't figure out how I can attach the MenuItem_Click event when a user click on different menu. I tried the following but it does not work:
    <telerik:RadMenu Grid.Row="1"
    Style
    ="{StaticResource MenuStyle}"
    ItemsSource
    ="{StaticResource MenuItemsSource}"
    ItemContainerStyleSelector
    ="{StaticResource MenuItemStyleSelector}"
    ItemContainerStyle
    ="{x:Null}"
    MenuItem.Click="MenuItem_Click"/>


    Any idea how I can attach the MenuItem_Click event ?

    Thanks.
  2. Answer
    Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 09 May 2011 Link to this post

    Hello Djuneid,

    RadMenu is using RadMenuItem as a children. So instead of MenuItem.Click you should use RadMenuItem.Click event.
    There is another option - we expose RadMenuItem.Click event through RadMenu.ItemClick event.
    So you could use either RadMenu ItemClick event or RadMenuItem.Click event.

    Let us know if you need more information.

    Kind regards,
    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Yas
    Yas avatar
    10 posts
    Member since:
    Jul 2009

    Posted 10 May 2011 Link to this post

    Hello Hristo,

    I made a silly mistake regarding the MenuItem.Clik Event. As you rightly pointed out, I have to use the RadMenuItem.Click Event.

    But now when I use the RadMenuItem.Click Event I cannot get the Item that has been Clicked. I manage to retrieve the latter by using the following workaround (I hope it might be helpful to somebody and it could save some time).

    (1) Attach a MouseUp Event (You can use MouseDown Event but I think it is better to use MouseUp) to the RadMenuItem as follows:

    <ControlTemplate x:Key="ParagraphImageMenuItemControlTemplate" 
            TargetType="telerik:RadMenuItem">
        <Grid Cursor="Hand" 
                                Background="#00888888" 
                                MouseUp="GridRadMenuItem_Click">

    (2) In the code-behind to get the Title of the RadMenuItem being clicked use the following code:

    //((sender as Grid).Children[0] contains the Image
    //((sender as Grid).Children[1] contains the Title
    //((sender as Grid).Children[2] contains the Summary
      
    //To get the Clicked RadMenuItem's Title
    ((sender as Grid).Children[1] as TextBlock).Text

    Using this method I know which RadMenuItem has been Clicked by retrieving the Title. If there is a better way then I'm listening :)

    Thanks for the help.
  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 11 May 2011 Link to this post

    Hello Yas,

    You can check which items was clicked by using the RadRoutedEventArgs OriginalSource property.
    No matter which event you attach you can always check the original source to get the item that fired clicked event. Here is a simple snippet (the first one is using RadMenu ItemClick event which the second is using RadMenuItem.Click routed event):

    private void RadMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        Debug.WriteLine(e.OriginalSource);
    }
      
    private void RadMenu_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        Debug.WriteLine(e.OriginalSource);
    }

    I hope that this will help you.

    Greetings,
    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
  6. Yas
    Yas avatar
    10 posts
    Member since:
    Jul 2009

    Posted 11 May 2011 Link to this post

    Hello Hristo,

    Thanks for the help. It was very helpful.

    Regards.
Back to Top
UI for WPF is Visual Studio 2017 Ready