views:

406

answers:

3

I'd like the main menu in my WPF app to behave like the main menu in IE8:

  • it's not visible when the app starts
  • pressing and releasing Alt makes it visible
  • pressing and releasing Alt again makes it invisible again
  • repeat until bored

How can I do this? Does it have to be code?

Added in response to answers submitted, because I'm still having trouble:

My Shell code-behind now looks like this:

public partial class Shell : Window
{
    public static readonly DependencyProperty IsMainMenuVisibleProperty;

    static Shell()
    {
        FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
        metadata.DefaultValue = false;

        IsMainMenuVisibleProperty = DependencyProperty.Register(
            "IsMainMenuVisible", typeof(bool), typeof(Shell), metadata);
    }

    public Shell()
    {
        InitializeComponent();

        this.PreviewKeyUp += new KeyEventHandler(Shell_PreviewKeyUp);
    }

    void Shell_PreviewKeyUp(object sender, KeyEventArgs e)
    {
        if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
        {
            if (IsMainMenuVisible == true)
                IsMainMenuVisible = false;
            else
                IsMainMenuVisible = true;
        }
    }

    public bool IsMainMenuVisible
    {
        get { return (bool)GetValue(IsMainMenuVisibleProperty); }
        set { SetValue(IsMainMenuVisibleProperty, value); }
    }
}
+1  A: 

I'd try looking into handling the PreviewKeyDown event on your window. I'm not sure if pressing Alt triggers this event or not, but if it does, then I'd toggle a bool which is bound to the visibility of the main menu of the window.

If PreviewKeyDown doesn't work, I'm not sure what else to try. You could look into getting at the actual Windows messages sent to your window, but that could get messy very quickly.

Andy
Thanks for the suggestion. I'm going with Phil Price's suggestion of PreviewKeyUp because that's closer to IE but otherwise it sounds doable.
serialhobbyist
+2  A: 

You can use the PreviewKeyDown event on the window. To detect the alt key you will need to check the SystemKey property of the KeyEventArgs, as opposed to the Key property which you normally use for most other keys.

You can use this event to set a bool value which has been declared as a DependencyProperty in the windows code behind.

The menu's Visibility property can then be bound to this property using the BooleanToVisibilityConverter.

<Menu 
    Visibility={Binding Path=IsMenuVisibile, 
        RelativeSource={RelativeSource AncestorType=Window},
        Converter={StaticResource BooleanToVisibilityConverter}}
    />
Ian Oakes
I can't get this working. I've updated the original question with the code-behind for my DependencyProperty. It's the first time I've declared my own so... I've added your binding to the Menu in Shell.xaml and the error I'm getting is: System.Windows.Data Error: 39 : BindingExpression path error: 'IsMenuVisibile' property not found on 'object' ''Shell' (Name='')'. BindingExpression:Path=IsMenuVisibile; DataItem='Shell' (Name=''); target element is 'Menu' (Name='uxMainMenu'); target property is 'Visibility' (type 'Visibility'). Any ideas?
serialhobbyist
This is the standard binding error you get when the property can't be found. To fix it you need to replace IsMenuVisible with IsMainMenuVisible in your binding.
Ian Oakes
Oh, I am such an idiot! I'll log in and sort it out. Thanks. And thanks for not pointing out how thick I am. :-)
serialhobbyist
That was exactly it. Thanks very much. Not only have I implemented (thanks to you) the Alt-key functionality AND my first DependencyProperty but I've also painted two ceilings today: I think I'll quite now, whilst I'm ahead. :-)
serialhobbyist
A: 

It would be better to use GetKeyboardState with VK_MENU to handle both left and right alt, to mimic the behavior of IE / Windows Explorer (Vista+) you'll need to track the previously focused element to store focus, on a VK_MENU press whilst the focused element is within your main menu. You also want to be doing this work on PreviewKeyUp (not down).

Phil Price
Thanks. At the mo, I can't even get the basics going so I'll leave WM stuff for a bit!
serialhobbyist