none
WPF中如何给菜单添加自定义快捷键? RRS feed

  • 问题

  • 在WPF中如何让用户自定义快捷键,这些快捷键保存到XML文件中,让程序启动时读取,然后设置菜单的快捷键。
    • 已移动 桂素伟Moderator 2009年5月19日 9:52 ([Loc]From:Windows Vista 开发相关讨论)
    2008年6月28日 8:36

答案

  • 参考

    WPF introduces commands, and this is the recommended way to associate a specified key combination with an action, here is an example:

    Code Block
    <Window x:Class="MenuItemCommandDemo.Window1"
    xmlns:local="clr-namespace:MenuItemCommandDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <
    Grid>
    <
    Menu>
    <
    MenuItem Header="_File">
    <
    MenuItem Header="_Start a process" Command="local:CustomCommands.StartProcCommand"/>
    <
    MenuItem Header="_Help" Command="ApplicationCommands.Help"/>
    </
    MenuItem>
    </
    Menu>
    </
    Grid>
    </
    Window>

    namespace
    MenuItemCommandDemo
    {
    public static class CustomCommands
    {
    private static RoutedUICommand startProcCommand;
    public static RoutedUICommand StartProcCommand
    {
    get
    {
    if (startProcCommand == null)
    {
    startProcCommand = new RoutedUICommand(
    "StartProc",
    "StartProc",
    typeof(Window1),
    new InputGestureCollection { new KeyGesture(Key.M, ModifierKeys.Control) });
    }

    return startProcCommand;
    }
    }
    }

    public partial class Window1 : Window
    {

    public Window1()
    {
    InitializeComponent();
    this.CommandBindings.Add(new CommandBinding(
    CustomCommands.StartProcCommand,
    (sender, e) =>
    {
    Process proc = new Process();
    proc.StartInfo = new ProcessStartInfo(@"c:\windows\notepad.exe", null);
    proc.Start();
    },
    (sender, e) =>
    {
    e.CanExecute = true;
    }));
    this.CommandBindings.Add(new CommandBinding(
    ApplicationCommands.Help,
    (sender, e) =>
    {
    MessageBox.Show("Help!");
    },
    (sender, e) =>
    {
    e.CanExecute = true;
    }));
    }
    }
    }


    The first thing you need to do is define a custom command which is associated with a specified KeyGuesture, then use CommandBinding to bind to command to a needed command handling code to implement the logic.

     

    2008年6月28日 13:23
    版主

全部回复

  • 参考

    WPF introduces commands, and this is the recommended way to associate a specified key combination with an action, here is an example:

    Code Block
    <Window x:Class="MenuItemCommandDemo.Window1"
    xmlns:local="clr-namespace:MenuItemCommandDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <
    Grid>
    <
    Menu>
    <
    MenuItem Header="_File">
    <
    MenuItem Header="_Start a process" Command="local:CustomCommands.StartProcCommand"/>
    <
    MenuItem Header="_Help" Command="ApplicationCommands.Help"/>
    </
    MenuItem>
    </
    Menu>
    </
    Grid>
    </
    Window>

    namespace
    MenuItemCommandDemo
    {
    public static class CustomCommands
    {
    private static RoutedUICommand startProcCommand;
    public static RoutedUICommand StartProcCommand
    {
    get
    {
    if (startProcCommand == null)
    {
    startProcCommand = new RoutedUICommand(
    "StartProc",
    "StartProc",
    typeof(Window1),
    new InputGestureCollection { new KeyGesture(Key.M, ModifierKeys.Control) });
    }

    return startProcCommand;
    }
    }
    }

    public partial class Window1 : Window
    {

    public Window1()
    {
    InitializeComponent();
    this.CommandBindings.Add(new CommandBinding(
    CustomCommands.StartProcCommand,
    (sender, e) =>
    {
    Process proc = new Process();
    proc.StartInfo = new ProcessStartInfo(@"c:\windows\notepad.exe", null);
    proc.Start();
    },
    (sender, e) =>
    {
    e.CanExecute = true;
    }));
    this.CommandBindings.Add(new CommandBinding(
    ApplicationCommands.Help,
    (sender, e) =>
    {
    MessageBox.Show("Help!");
    },
    (sender, e) =>
    {
    e.CanExecute = true;
    }));
    }
    }
    }


    The first thing you need to do is define a custom command which is associated with a specified KeyGuesture, then use CommandBinding to bind to command to a needed command handling code to implement the logic.

     

    2008年6月28日 13:23
    版主
  • 首先感谢你的回复,但是这个是程序员定义的快捷键,用户无法修改,我的目的是让用户自定义。
    2008年6月29日 13:01