WPF Menu keyboard navigation problems - wpf

I've got a WPF program containing a simple Menu.
When The keyboard focus is set into the menu, in Win32 I was able to jump to menu items by typing the first character of the MenuItem name.
In WPF menus this does not work anymore, unless I mark the first character of the menu item using an underscore.
Is this a bug or a feature?

This is supported in wpf also.It is done by adding an underscore in front of a character.
(The ampersand does not work in WPF!).Check the below sample
<Menu Height="22" Name="menu1" Width="200" Margin="10, 10, 5, 5" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="2">
<MenuItem Header="_File">
<MenuItem Header="_Open" IsCheckable="False">
<MenuItem Header="_One" IsCheckable="True"/>
<MenuItem Header="_Two" IsCheckable="True"/>
</MenuItem>
<MenuItem Header="_Close" IsCheckable="True"/>
<MenuItem Header="_Save" IsCheckable="True"/>
</MenuItem>
</Menu>
The underlined characters show up when you press the Alt key to access the menu. You can then navigate the menu by pressing the underlined characters

Related

How to create a menu at the bottom of a metro window in WPF

I want to create a menu at the bottom of a metro window. How should I do it?
Attached image of what I am looking for. Gray area at bottom, over which will have buttons and click of button, popup will be shown with options.
Please suggest.
Set the vertical alignment of menu to bottom
e.g. :
<Menu Height="23" HorizontalAlignment="Stretch" Margin="86,0,0,0" Name="Menu1" VerticalAlignment="Bottom" Width="334">
<MenuItem Header=" My"/>
<MenuItem Header=" My option 2"/>
<MenuItem Header=" My option 3"/>
</Menu>

Command doesn't work on top level MenuItem in MVVM

It works fine if the MenuItem doesn't have a sub MenuItem, like this:
<MenuItem Header="Open" Command="{Binding OpenCommand}"/>
but, when I add a sub MenuItem to it, the Command doesn't work:
<MenuItem Header="Open" Command="{Binding OpenCommand}">
<MenuItem />
</MenuItem>
click event also doesn't work like this:
<MenuItem Header="Open" Click="MenuItem_Click">
<MenuItem />
</MenuItem>
when I try to add the Command to the header:
<MenuItem>
<MenuItem.Header>
<TextBlock>
<TextBlock.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding OpenCommand}"/>
</TextBlock.InputBindings>
Open
</TextBlock>
</MenuItem.Header>
<MenuItem />
</MenuItem>
the Command works but the sub MenuItem doesn't show.
Any help will be appreciate, and forgive my half-baked English.
If you have sub MenuItems the click event is used (and expected by the user) to show the sub-menu. To react upon a submenu opening use the event SubmenuOpened.
If you really want to be able to open a sub menu and click on the "Open", you could use this snipper, but I'd really not advice it:
<MenuItem SubmenuOpened="MenuItem_OnSubmenuOpened"> <!-- handle sub menu opening if desired -->
<MenuItem.Header>
<Button Click="Button_Click">Open V2</Button> <!-- handle click on "Open" if desired; doesn't open sub menu! -->
</MenuItem.Header>
<MenuItem />
</MenuItem>
Note that I have tested this with "Open" not being the top level menu item.

How to apply a custom style context menu for TextBox (default context menu copy/cut/paste)?

I create style for the MenuItem.
When I create my own menu, everything is good.
But how can I apply a style to the context menu of TextBox?
I mean, the menu (Copy, Cut, Paste, etc.) - I do not want to create new lines, and just change the style.
//My Custom Menu Example
<UserControl.ContextMenu>
<ContextMenu Style="{StaticResource ContextMenuStyle}" HasDropShadow="True">
<MenuItem x:Name="MenuItem1" Header="Open"
Style="{StaticResource ContextMenuItem}">
</MenuItem>
<MenuItem x:Name="MenuItem2" Header="Save"
Style="{StaticResource ContextMenuItem}">
</MenuItem>
<MenuItem x:Name="MenuItem3" Header="Delete"
Style="{StaticResource ContextMenuItem}">
</MenuItem>
</ContextMenu>
</UserControl.ContextMenu>
You will technically have to to do the same thing you did to your UserControl.ContextMenu to the TextBox.ContextMenu.
Look the below link which has exactly the scenario you are asking about with xaml.

WPF menu tab navigation

I have a view with a menu:
<Menu IsTabStop="False">
<MenuItem
Header="_File"
IsTabStop="True">
<MenuItem
Header="New / Start Over"
IsTabStop="True"
InputGestureText="Ctrl+N"
Command="{x:Static common:Commands.StartOverCommand}" />
<Separator
IsTabStop="False" />
<MenuItem
Header="Log Out"
IsTabStop="True"
InputGestureText="F12"
Click="LogoutMenuItem_Click" />
<MenuItem
Header="E_xit"
IsTabStop="True"
InputGestureText="Alt+F4"
Click="ExitMenuItem_Click" />
</MenuItem>
<MenuItem
Header="_Edit"
IsTabStop="True">
<MenuItem
Header="Undo Edit Field"
IsTabStop="True"
InputGestureText="Ctrl+Z"
Click="_undoMenuItem_Click" />
<MenuItem
Header="Redo Edit Field"
IsTabStop="True"
InputGestureText="Ctrl+Y"
Click="_redoMenuItem_Click" />
</MenuItem>
<MenuItem
Header="_Tools"
IsTabStop="True">
<MenuItem
Header="Comments"
IsTabStop="True"
InputGestureText="Ctrl+M"
Click="_commentsMenuItem_Click" />
</MenuItem>
</Menu>
I would like to be able to navigate through the menus (File, Edit, Tools) by using the right and left arrow keys, which I was able to accomplish by making these MenuItems IsTabStop="True". But since I did this I am able to tab to the menu as I cycle through the other valid fields on a page. When I use Alt+F to put focus on the File menu I want to be able to cycle through the 3 menus with the arrow keys, but I don't want to be able to tab to these menus from the page. How would I accomplish this?
Use the KeyboardNavigation.TabNavigation Attached Property with KeyboardNavigationMode.None on menu.
<Menu KeyboardNavigation.TabNavigation="None">

How to create a right-click context menu for a button in WPF

i know how to create a left-click context menu for a button, but I am not too sure how to do it for a right click? (i.e. how to specify that the context menu should appear on a right-click, not a left click).
Many thanks.
Here is a sample:
<Button Content="button" Name="btn" Width="100">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Cut"/>
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
</ContextMenu>
</Button.ContextMenu>
</Button>

Resources