Button Click Event Getting Lost

Posted by AlishahNovin on Stack Overflow See other posts from Stack Overflow or by AlishahNovin
Published on 2010-05-10T14:50:24Z Indexed on 2010/05/10 14:54 UTC
Read the original article Hit count: 438

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

I can't put any checks inside the LostFocus() event to see if my SubMenuBtn has focus, because it does not gain focus until after the LostFocus() event is called. In other words, SubMenuBtn.IsFocused = false when LostFocus() is triggered.

Anyone have any thoughts about this?

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about button