0

我有一个示例 WPF 应用程序,我试图让 KeyGestures 工作的应用程序级菜单。如果我在 MainWindow 中有我的应用程序菜单,这可以正常工作。

这是我的窗口 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" />

4

1 回答 1

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/

于 2013-05-08T22:37:29.660 回答