我有一個示例WPF應用程序,我試圖讓KeyGestures工作的應用程序級菜單。 這是工作正常,如果我有我的應用程序菜單在MainWindow中。WPF CommandBinding爲應用程序菜單與手勢
這是我窗口的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>
在上面的代碼,所有的評論都是我的第一次嘗試,工作正常。
當我將菜單移至TopMenu控件時,出現問題。手勢不會觸發呼叫。
這是我的代碼背後。
/// <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>
問題是什麼在這裏,當我提出的代碼到用戶控件?僅供參考,在我移動代碼ApplicationCommands.New後,即使單擊菜單也不起作用。
注意:你必須移動從TopMenu向窗口2代碼隱藏,取消所有評論的XAML代碼中的第一個工作版本,評論<local:TopMenu DockPanel.Dock="Top" />