RadMenu.ItemClick - Can't close.

7 posts, 0 answers
  1. Steele
    Steele avatar
    12 posts
    Member since:
    Nov 2011

    Posted 10 Oct 2012 Link to this post

    Unable to close the entire menu when a header item is clicked while handling the ItemClick event.

    private void MnuViewsOnItemClick(object sender, RadRoutedEventArgs radRoutedEventArgs)
            {
                var currentItem = radRoutedEventArgs.OriginalSource as RadMenuItem;
     
                if (currentItem != null)
                {
                    var viewMenuItem = currentItem.Header as ViewMenuItem;
     
                    if (viewMenuItem == null || viewMenuItem.Id == 0)
                    {
                        return;
                    }
     
                    RadMenuItemAutomationPeer CloseME = new RadMenuItemAutomationPeer(currentItem);
                    CloseME.Invoke();
                }
            }



  2. Lancelot
    Lancelot avatar
    251 posts
    Member since:
    Jul 2012

    Posted 10 Oct 2012 Link to this post

    Hi Steele,

    You can find a solution to this issue at this thread. At the last post in the thread, George attaches a sample project and video that demonstrates the solution.

    Good Luck,
    Lancelot
  3. DevCraft banner
  4. Steele
    Steele avatar
    12 posts
    Member since:
    Nov 2011

    Posted 10 Oct 2012 Link to this post

    I don't believe that solution applies to my problem. I'm not blocking any threads in my solution. And the menu behaves appropriately when clicking on a node that has no children. If you click on a node that is a parent the menu will not close.
  5. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 11 Oct 2012 Link to this post

    Hi,

    The default behavior when you click on a RadMenuItem that doesn't have children, is to close the opened menu(if the property StaysOpenOnClick is not set to true). That's why when you click on an item without children it is closing all items.Your code is just not closing the menu, it's the default behavior that is doing it.

    To close the items you can use the code below in your ItemClick event handler:
    while (currentItem != null)
    {
        RadMenuItemAutomationPeer CloseME = new RadMenuItemAutomationPeer(currentItem);
     
        if (currentItem.HasItems && currentItem.IsSubmenuOpen == true)
        {
            CloseME.Collapse(); //collapse the children
        }
     
        currentItem = currentItem.Parent as RadMenuItem;
    }

    It is collapsing the children of the current clicked item, finding the parent of the clicked item, collapsing its children, etc. Note that you'll have to set NotifyOnHeaderClick on the RadMenu to True if you want the ItemClick event to be fired when you click on an item that has children. I'm sending you a simple project demonstrating the usage of the code.

    Hopefully this helps.

    Regards,
    Rosen Vladimirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Steele
    Steele avatar
    12 posts
    Member since:
    Nov 2011

    Posted 11 Oct 2012 Link to this post

    Thank you for your reply. I have updated my code to reflect yours but, I still am not acheiving the desired results. Please see this video showing that only the first set of children are collapsed and then the process stops. I need the entire menu to collapse.

    Here is my updated code:
    private async void MnuViewsOnItemClick(object sender, RadRoutedEventArgs radRoutedEventArgs)
            {
                var currentItem = radRoutedEventArgs.OriginalSource as RadMenuItem;
     
                if (currentItem != null)
                {
                    var viewMenuItem = currentItem.Header as ViewMenuItem;
     
                    if (viewMenuItem == null || viewMenuItem.Id == 0)
                    {
                        return;
                    }
     
                    while (currentItem != null)
                    {
                        RadMenuItemAutomationPeer CloseME = new RadMenuItemAutomationPeer(currentItem);
     
                        if (currentItem.HasItems && currentItem.IsSubmenuOpen == true)
                        {
                            CloseME.Collapse(); //collapse the children
                        }
     
                        currentItem = currentItem.Parent as RadMenuItem;
                    }
     
                    LiveWireBusyIndicator.IsBusy = true;
     
                    _dataPointsOperation = await _context.Load(
                        _context.GetDataPointsQuery(_user.Id,
                            _user.UserRoleMappings.First().LiveWireRoleId,
                            _user.DefaultDataSet, viewMenuItem.Id, true)
                    ).AsTask();
     
                    await InitializeDataPoints();
     
                    LiveWireBusyIndicator.IsBusy = false;
                }
            }

    And here is a link to the video: http://www.thelyndco.com/download/menuWontClose.avi
  7. Steele
    Steele avatar
    12 posts
    Member since:
    Nov 2011

    Posted 11 Oct 2012 Link to this post

    I have found a solution to my problem. Please review my code and let me know if this is the most effiecient way of acheiving my goal.

    private async void MnuViewsOnItemClick(object sender, RadRoutedEventArgs radRoutedEventArgs)
            {
                var currentItem = radRoutedEventArgs.OriginalSource as RadMenuItem;
     
                if (currentItem == null) return;
     
                var viewMenuItem = currentItem.Header as ViewMenuItem;
     
                if (viewMenuItem == null || viewMenuItem.Id == 0)
                {
                    return;
                }
     
                foreach (var closeMe in
                    currentItem.GetParents().OfType<RadMenuItem>()
                    .Select(parent => new RadMenuItemAutomationPeer(parent as RadMenuItem)))
                {
                    closeMe.Collapse();
                }
     
                LiveWireBusyIndicator.IsBusy = true;
     
                _dataPointsOperation = await _context.Load(
                    _context.GetDataPointsQuery(_user.Id,
                                                _user.UserRoleMappings.First().LiveWireRoleId,
                                                _user.DefaultDataSet, viewMenuItem.Id, true)
                                                 ).AsTask();
     
                await InitializeDataPoints();
     
                LiveWireBusyIndicator.IsBusy = false;
            }
  8. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 12 Oct 2012 Link to this post

    Hi,

    You are correct, the Parent is not always RadMenuItem and that's why your menu didn't close.

    For your solution - it seems ok, in fact this would have been my suggestion - to use ParentOfType<RadMenuItem>.

    Don't hesitate to contact us if you have any other problems or concerns regarding RadControls.

    All the best,
    Rosen Vladimirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft banner