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

MenuItem_Click Event for Custom Menu

4 Answers 214 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Yas
Top achievements
Rank 1
Yas asked on 09 May 2011, 07:16 AM
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.

4 Answers, 1 is accepted

Sort by
0
Accepted
Hristo
Telerik team
answered on 09 May 2011, 08:05 AM
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
0
Yas
Top achievements
Rank 1
answered on 10 May 2011, 02:47 PM
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.
0
Hristo
Telerik team
answered on 11 May 2011, 07:19 AM
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
0
Yas
Top achievements
Rank 1
answered on 11 May 2011, 05:44 PM
Hello Hristo,

Thanks for the help. It was very helpful.

Regards.
Tags
Menu
Asked by
Yas
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Yas
Top achievements
Rank 1
Share this question
or