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

Menu with XML source

5 Answers 94 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 2
Charles asked on 10 Nov 2008, 07:39 PM
I have been able to get a menu to display using XML as the data source. (I used the sample in your help file.)  However, I'm not sure how to assign a click event to the menu items. I see other posts related to assigning the click event but those seem to be related to menus that are not dynamically created. Also, I don't see any way to get to the "AddHandler" method.

So, how to assign a single click event to the menu items of a dynamically created menu.

5 Answers, 1 is accepted

Sort by
0
Serrin
Top achievements
Rank 1
answered on 10 Nov 2008, 08:57 PM
Hey Charles,

Due to time constraints I wasn't able to work up any good sample code, but here's the idea...

Check out this link from the documentation for loading XML data to the menu:

http://www.telerik.com/help/silverlight/radmenu-binding-to-xml.html

Now... I'm by no means proficient with Silverlight yet so not sure how well this would work (this is based off a javascript option with similar logic that I worked on), but you set the menu items to a generic RadMenuItem_Click:

private

 

void RadMenuItem_Click(object sender, RoutedEventArgs e)

And in that function you do custom logic based on the header of the sender. 

Not sure if anyone else has done something like this and has code (or a better suggestion), just throwing out my first idea when I read this post.  Still getting used to Silverlight/WPF so I'm always looking for better ways to handle things. :)

 

0
Charles
Top achievements
Rank 2
answered on 10 Nov 2008, 09:02 PM
That looks like it might work...but how to wire up the RadMenuItem_Click event to the menu? I can't seem to find that missing piece.
0
Accepted
Hristo
Telerik team
answered on 11 Nov 2008, 08:28 AM
Hello Charles,

RadControls for Silverlight 2 support RoutedEvents so this mean that you can handle event at some parent.
Let say that you have RadMenu with ItemsSource. That way you don't have the RadMenuItems (they are dynamically created when needed) so you can't add handler to Click event.
But you can add handler on RadMenu because RadMenuItem.Click event is routed event that bubbles (go from RadMenuItem to the Page/Root element).

Here is how you can attach handler in code from RadMenuItem.Click event.

public partial class Page : UserControl  
{  
    public Page()  
    {  
        InitializeComponent();  
        //using Telerik.Windows;  
        //using Telerik.Windows.Controls;  
        radMenu1.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuClick));  
        //radMenu1 is Declared in Page XAML  
    }  
 
    private void OnMenuClick(object sender, RoutedEventArgs e)  
    {  
        RadRoutedEventArgs args = e as RadRoutedEventArgs;  
        RadMenuItem item = args.OriginalSource as RadMenuItem;  
        string header = Convert.ToString(item.Header);  
        if (header == "Close")  
        {  
            //Do something  
        }  
    }  


AddHandler is extension method so you will need the commented usings (using Telerik.Windows).
The cast from RouthedEventArgs to RadRouthedEventArgs is needed because RouthedEventArgs OriginalSource is readonly and the handler for Click event is of RoutedEventHandler type (we may change the delegate in future).



Regards,
Hristo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Charles
Top achievements
Rank 2
answered on 11 Nov 2008, 03:29 PM
Thanks. That worked.
0
Serrin
Top achievements
Rank 1
answered on 11 Nov 2008, 05:23 PM
Wow, tons nicer than what I came up with.  Bookmarking this answer for sure. ;D
Tags
Menu
Asked by
Charles
Top achievements
Rank 2
Answers by
Serrin
Top achievements
Rank 1
Charles
Top achievements
Rank 2
Hristo
Telerik team
Share this question
or