Menu with XML source

6 posts, 1 answers
  1. Charles
    Charles avatar
    75 posts
    Member since:
    Aug 2008

    Posted 10 Nov 2008 Link to this post

    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.

  2. Serrin
    Serrin avatar
    286 posts
    Member since:
    Sep 2008

    Posted 10 Nov 2008 Link to this post

    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. :)

     

  3. DevCraft banner
  4. Charles
    Charles avatar
    75 posts
    Member since:
    Aug 2008

    Posted 10 Nov 2008 Link to this post

    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.
  5. Answer
    Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 11 Nov 2008 Link to this post

    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.
  6. Charles
    Charles avatar
    75 posts
    Member since:
    Aug 2008

    Posted 11 Nov 2008 Link to this post

    Thanks. That worked.
  7. Serrin
    Serrin avatar
    286 posts
    Member since:
    Sep 2008

    Posted 11 Nov 2008 Link to this post

    Wow, tons nicer than what I came up with.  Bookmarking this answer for sure. ;D
Back to Top
DevCraft banner