如何设置菜单的快捷键?

时间:2020-12-06 14:15:03

How can I set shortKey for menu for example , for this code :

如何为此代码设置菜单的shortKey:

<Menu >
    <MenuItem Header="File"  >
        <MenuItem Header="Save" ToolTip="Ctrl + S" Click="Save_Click"/>
        <MenuItem Header="Save As" ToolTip="Ctrl + S + Shift" Click="SaveAs_Click"/>
        <MenuItem Header="SelectAll" ToolTip="Ctrl + A" Click="SelectAll_Click"/>
    </MenuItem>
</Menu>

In other word when I pressed Ctrl-S , Save_Click would be raise and so on.

换句话说,当我按下Ctrl-S时,Save_Click会被提升,依此类推。

2 个解决方案

#1


2  

As Igor said. However with you wish to avoid writing your own ICommand implementation you can add a CommandBinding for the Save and SaveAs commands.

正如伊戈尔所说。但是,如果您希望避免编写自己的ICommand实现,可以为Save和SaveAs命令添加CommandBinding。

<Window x:Class="MenuShotCuts.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Save"
                        Executed="SaveCommandHandler"
                    />
   </Window.CommandBindings>
   <!-- implementation -->
</Window>

You'll need to change the Command property of the MenuItem as well:

您还需要更改MenuItem的Command属性:

<MenuItem Header="Save" ToolTip="Ctrl + S" Command="ApplicationCommands.Save"/>

In the code behind:

在后面的代码中:

// Save executed handler
private void SaveCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
    // do something
}

#2


3  

I think the best way is to use WPF Command pattern

我认为最好的方法是使用WPF Command模式

<Window x:Class="MenuShotCuts.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.InputBindings>
        <KeyBinding Key="S" Modifiers="Ctrl"  Command="{Binding SaveCmd}" />
    </Window.InputBindings>
    <Grid>
        <Menu >
            <MenuItem Header="File"  >
                <MenuItem Header="Save" ToolTip="Ctrl + S" Command="{Binding SaveCmd}"/>
            </MenuItem>
        </Menu>
    </Grid>
</Window>

In code behind:

代码背后:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;

        m_saveCmd = new SaveCommand();
    }

    private SaveCommand m_saveCmd;
    public SaveCommand SaveCmd
    {
        get
        {
            return m_saveCmd;
        }
    }
}

public class SaveCommand : ICommand
{
    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        MessageBox.Show("Saved", "Info");
    }

    #endregion
}

#1


2  

As Igor said. However with you wish to avoid writing your own ICommand implementation you can add a CommandBinding for the Save and SaveAs commands.

正如伊戈尔所说。但是,如果您希望避免编写自己的ICommand实现,可以为Save和SaveAs命令添加CommandBinding。

<Window x:Class="MenuShotCuts.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Save"
                        Executed="SaveCommandHandler"
                    />
   </Window.CommandBindings>
   <!-- implementation -->
</Window>

You'll need to change the Command property of the MenuItem as well:

您还需要更改MenuItem的Command属性:

<MenuItem Header="Save" ToolTip="Ctrl + S" Command="ApplicationCommands.Save"/>

In the code behind:

在后面的代码中:

// Save executed handler
private void SaveCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
    // do something
}

#2


3  

I think the best way is to use WPF Command pattern

我认为最好的方法是使用WPF Command模式

<Window x:Class="MenuShotCuts.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.InputBindings>
        <KeyBinding Key="S" Modifiers="Ctrl"  Command="{Binding SaveCmd}" />
    </Window.InputBindings>
    <Grid>
        <Menu >
            <MenuItem Header="File"  >
                <MenuItem Header="Save" ToolTip="Ctrl + S" Command="{Binding SaveCmd}"/>
            </MenuItem>
        </Menu>
    </Grid>
</Window>

In code behind:

代码背后:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;

        m_saveCmd = new SaveCommand();
    }

    private SaveCommand m_saveCmd;
    public SaveCommand SaveCmd
    {
        get
        {
            return m_saveCmd;
        }
    }
}

public class SaveCommand : ICommand
{
    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        MessageBox.Show("Saved", "Info");
    }

    #endregion
}