我有一个示例WPF应用程序,我正在尝试使用KeyGestures工作的应用程序级别菜单。 如果我在MainWindow中有我的应用程序菜单,这工作正常。
这是我的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>
在上面的代码中,所有评论是我的第一次尝试,它工作正常。
当我将菜单移动到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>
将代码移动到UserControl时会出现什么问题?仅供参考,我移动代码ApplicationCommands.New即使单击菜单也无法正常工作。
注意:对于第一个工作版本,您必须将代码从TopMenu移动到Window2代码隐藏,取消注释所有注释的Xaml,注释<local:TopMenu DockPanel.Dock="Top" />
答案 0 :(得分:1)
我想我得到了答案。如果有人有更好的方法,请告知。
我必须将此代码添加到主窗口。 CommandTarget是将其转发给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>
感谢您的时间。
这就是给我答案的原因,但我是在Xaml中做到的。
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/753c2a0b-753f-43d3-afb3-01d4d3c93787/