Reputation: 785
I have a sample WPF app that I am trying to get the App level menus with KeyGestures working. This is working fine if I have my app menu in the MainWindow.
Here is my Window Xaml
<Window x:Class="SampleWpfApp.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SampleWpfApp"
Name="RootWindow"
Title="Window2" Height="600" Width="800">
<!--<Window.InputBindings>
<KeyBinding Gesture="CTRL+N" Command="{Binding ApplicationCommands.New}" />
<KeyBinding Gesture="CTRL+F1" Command="{x:Static local:Window2.ShowHelp}" />
</Window.InputBindings>
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.New"
Executed="NewExecuted"
CanExecute="NewCanExecute"/>
<CommandBinding x:Name="HelpCmdBinding" CanExecute="AltHelpCanExecute" Executed="AltHelpExecuted" Command="{x:Static local:Window2.ShowHelp}" />
</Window.CommandBindings>-->
<DockPanel>
<!--<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New" InputGestureText="Ctrl+N" Command="{Binding ApplicationCommands.New}" />
<MenuItem Header="E_xit" InputGestureText="Alt+F4" />
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_View Help" InputGestureText="Ctrl+F1" Command="{x:Static local:Window2.ShowHelp}" />
<MenuItem Header="_About" />
</MenuItem>
</Menu>-->
<local:TopMenu DockPanel.Dock="Top" />
<ContentControl>
</ContentControl>
</DockPanel>
In the above code, all commented are my first try which works fine.
When I moved my Menu to TopMenu control I have an issue. Gestures don't fire the call.
Here is my code behind.
/// <summary>
/// Interaction logic for TopMenu.xaml
/// </summary>
public partial class TopMenu : UserControl
{
public static RoutedCommand ShowHelp = new RoutedCommand("AltHelp", typeof(TopMenu));
public TopMenu()
{
InitializeComponent();
}
void NewExecuted(object target, ExecutedRoutedEventArgs e)
{
MessageBox.Show("The " + ((RoutedCommand)e.Command).Name + " command invoked on " + ((FrameworkElement)target).Name);
}
void NewCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
void AltHelpExecuted(object target, ExecutedRoutedEventArgs e)
{
MessageBox.Show("The " + ((RoutedCommand)e.Command).Name + " command invoked on " + ((FrameworkElement)target).Name);
}
void AltHelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
}
<UserControl x:Class="SampleWpfApp.TopMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SampleWpfApp"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.InputBindings>
<KeyBinding Gesture="CTRL+N" Command="{Binding ApplicationCommands.New}" />
<KeyBinding Gesture="CTRL+F1" Command="{x:Static local:TopMenu.ShowHelp}" />
</UserControl.InputBindings>
<UserControl.CommandBindings>
<CommandBinding Command="ApplicationCommands.New"
Executed="NewExecuted"
CanExecute="NewCanExecute"/>
<CommandBinding x:Name="HelpCmdBinding" CanExecute="AltHelpCanExecute" Executed="AltHelpExecuted" Command="{x:Static local:TopMenu.ShowHelp}" />
</UserControl.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New" InputGestureText="Ctrl+N" Command="{Binding ApplicationCommands.New}" />
<MenuItem Header="E_xit" InputGestureText="Alt+F4" />
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_View Help" InputGestureText="Ctrl+F1" Command="{x:Static local:TopMenu.ShowHelp}" />
<MenuItem Header="_About" />
</MenuItem>
</Menu>
</DockPanel>
What is the issue here when I moved the code to the UserControl? FYI, after I moved the code ApplicationCommands.New does not work at all even when I click the menu.
Note: For the first working version you have to move the code from TopMenu to Window2 codebehind, uncomment all commented Xaml, comment <local:TopMenu DockPanel.Dock="Top" />
Upvotes: 0
Views: 1662
Reputation: 785
I think I got the answer. If anyone has a better way of doing please advise.
I had to add this code to the main window. The CommandTarget is the one which forwards it to the TopMenu.
<Window.InputBindings>
<KeyBinding Gesture="CTRL+N" Command="ApplicationCommands.New" CommandTarget="{Binding ElementName=TopMenu}" />
<KeyBinding Gesture="CTRL+F1" Command="{x:Static local:TopMenu.ShowHelp}" CommandTarget="{Binding ElementName=TopMenu}" />
</Window.InputBindings>
Thanks for your time.
This is what gave me the answer but I did it in Xaml.
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/753c2a0b-753f-43d3-afb3-01d4d3c93787/
Upvotes: 1