Get the Entity object associated with a clicked RadMenu item

7 posts, 1 answers
  1. jay
    jay avatar
    39 posts
    Member since:
    Jun 2010

    Posted 09 Nov 2011 Link to this post

    I have a RadMenu that gets its items from a database. Below is the code that correctly populates the items.
    What i need is to be able to extract the Report object of type lu_Reports in the Menu ItemClick Event (part of which is also shown below). I can get the name of the report but i need to have the SelectedReport object which i have bound to another control on the page.

     

    private void LoadMenuItems(ObservableCollection<lu_Reports> myRptList)
            {
                RadMenuItem parentItem;
                RadMenuItem childItem;
                                         
                try
                {
                    if (myRptList != null)
                    {
                        foreach (var lu_Modules in _viewModel.ModulesList)
                        {
                            parentItem = new RadMenuItem() { Header = lu_Modules.ModuleName };
                            rptMenu.Items.Add(parentItem);
     
                            foreach (var obj in myRptList.Where(e=> e.ModuleID==lu_Modules.ModuleID))
                            {
                                childItem = new RadMenuItem() { Header = obj.ReportDescription, Name = obj.ReportName };
                                parentItem.Items.Add(childItem);
                            }
                        }
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
     
     
    private void radMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
            {
                RadMenuItem item = e.OriginalSource as RadMenuItem;
                if (item != null)
                {
    }
    }

     

     

  2. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 14 Nov 2011 Link to this post

    Hello Jay,

    You can create a class which inherits RadMenuItem and also has additional property that holds the corresponding Report:

    public class ExtendedMenuItem : RadMenuItem
    {
        public lu_Reports Report { set; get; }
    }

    then you can add ExtendedMenuItem as items of the menu - the Report can be easily accessed in the ItemClick event handler:

    private void rptMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        var report = (e.OriginalSource as ExtendedMenuItem).Report;
    }

    Hope this helps.

    Greetings,
    Yana
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. jay
    jay avatar
    39 posts
    Member since:
    Jun 2010

    Posted 14 Nov 2011 Link to this post

    Thanks, Yana.

    I am however getting a nullexception error in the Item_Click event. How do i use an instance of the new class in this event?

     

    Jay

     

     

     

    ExtendedMenuItem rpt = new ExtendedMenuItem();

     

  5. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 15 Nov 2011 Link to this post

    Hi Jay,

    I've attached a simple example to demonstrate the approach. Please download it and give it a try.

    Regards,
    Yana
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. jay
    jay avatar
    39 posts
    Member since:
    Jun 2010

    Posted 15 Nov 2011 Link to this post

    Hi Yana,

    I can't see your attachment.

    Thanks
    Jay
  7. Answer
    Yana
    Admin
    Yana avatar
    4554 posts

    Posted 15 Nov 2011 Link to this post

    Hi Jay,

    I'm sorry. Here is the attachment.

    Kind regards,
    Yana
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  8. jay
    jay avatar
    39 posts
    Member since:
    Jun 2010

    Posted 15 Nov 2011 Link to this post

    Thanks, Yana !
    That worked.

    Jay
Back to Top
DevCraft banner