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

RadMenu.ItemClick - Can't close.

6 Answers 110 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Steele
Top achievements
Rank 1
Steele asked on 10 Oct 2012, 05:21 PM
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();
            }
        }



6 Answers, 1 is accepted

Sort by
0
Lancelot
Top achievements
Rank 1
answered on 10 Oct 2012, 08:15 PM
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
0
Steele
Top achievements
Rank 1
answered on 10 Oct 2012, 08:29 PM
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.
0
Rosen Vladimirov
Telerik team
answered on 11 Oct 2012, 02:02 PM
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.

0
Steele
Top achievements
Rank 1
answered on 11 Oct 2012, 03:51 PM
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
0
Steele
Top achievements
Rank 1
answered on 11 Oct 2012, 09:15 PM
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;
        }
0
Rosen Vladimirov
Telerik team
answered on 12 Oct 2012, 05:46 AM
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.

Tags
Menu
Asked by
Steele
Top achievements
Rank 1
Answers by
Lancelot
Top achievements
Rank 1
Steele
Top achievements
Rank 1
Rosen Vladimirov
Telerik team
Share this question
or