Button Click Event Getting Lost

Posted by AlishahNovin on Stack Overflow See other posts from Stack Overflow or by AlishahNovin
Published on 2010-05-07T20:20:20Z Indexed on 2010/05/07 20:28 UTC
Read the original article Hit count: 458

Filed under:
|
|
|

I have a Menu and Submenu structure in Silverlight, and I want the submenu to disappear when the parent menu item loses focus - standard Menu behavior. I've noticed that the submenu's click events are lost when a submenu item is clicked, because the parent menu item loses focus and the submenu disappears.

It's easier to explain with code:

        ParentMenuBtn.Click += delegate
        {
            SubMenu.Visibility = (SubMenu.Visibility == Visibility.Visible) ? SubMenu.Collapsed : SubMenu.Visible;
        };
        ParentMenuBtn.LostFocus += delegate
        {
            SubMenu.Visibility = Visibility.Collapsed;
        };
        SubMenuBtn.Click += delegate
        {
            throw new Exception("This will never be thrown.");
        };

In my example, when SubMenuBtn is clicked, the first event that triggers is ParentMenuBtn.LostFocus(), which hides the container of SubMenuBtn. Once the container's visibility collapses, the Click event is never triggered.

I'd rather avoid having to hide the sub-menu each time, but I'm a little surprised that the Click event is never triggered as a result...

Anyone have any thoughts about this?

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about button