2009-09-10 96 views
0

我正在用一些命令编写一个WPF应用程序,所以我使用了一个没有菜单的工具栏。我想为工具栏上的按钮分配ctrl键快捷键。有没有简单的方法来做到这一点,而不必创建路由命令或ICommands来支持快捷方式?我宁愿用XAML来做,而不是代码隐藏。工具栏中的键盘快捷键

回答

0

我得出的结论是,WPF不提供一种简单的方法将Ctrl键添加到按钮。我通过在代码隐藏中捕获按键,然后调用相应的ICommand(相应按钮的Command属性绑定的ICommand)来解决我的问题。这是一个丑陋的黑客,但它的工作原理,它似乎是在这种情况下最好的方法。

+2

根据你对我的回答的评论,我不确定我是否理解你的问题。但我认为你的过度使用它。只要您使用的控件具有Command属性,WPF CommandBindings就可以完成您所需要的功能。您只需设置ApplicationCommand并且它们的键盘手势会自动设置为它。在我的例子中,如果你做Ctrl + O,CommandBinding_Executed会被触发。 – Carlo 2009-09-11 22:22:26

1

您确实需要代码,一个事件告诉命令它是否可以执行(通常不超过几行),另一个事件要做,所以绑定到它的每个控件都执行相同的操作。这里是一个非常简单的例子:

XAML:

<UserControl x:Class="WPFTests.AppCommands" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300" Width="300"> 
    <UserControl.CommandBindings> 
     <CommandBinding Command="ApplicationCommands.New" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" /> 
    </UserControl.CommandBindings> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <CheckBox Grid.Row="0" Height="16" Name="checkBoxCanExecute" Margin="8" IsChecked="true">Can execute</CheckBox> 
     <Button Grid.Row="1" Height="24" Padding="8,0,8,0" HorizontalAlignment="Left" Margin="8" Command="ApplicationCommands.New">ApplicationCommand.New</Button> 
    </Grid> 
</UserControl> 

C#中使用System.Windows

; using System.Windows.Controls; using System.Windows.Input;

namespace WPFTests 
{ 
    /// <summary> 
    /// Interaction logic for AppCommands.xaml 
    /// </summary> 
    public partial class AppCommands : UserControl 
    { 
     public AppCommands() 
     { 
      InitializeComponent(); 
     } 

     private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
     { 
      e.CanExecute = (bool)checkBoxCanExecute.IsChecked; 
     } 

     private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) 
     { 
      MessageBox.Show("New executed"); 
     } 
    } 
} 

编辑:由理查德的要求。以下是我在我的评论中发布的一些信息:

<Button Command="ApplicationCommands.New"> 

这会在按钮上放置键盘手势Ctrl + N.如果你要这样做

<Button Command="ApplicationCommands.Open"> 

它会把Ctrl + O.这隐式地在WPF的CommandBindings中实现。你甚至可以用自己的手势创建自己的命令。如果你绑定的命令添加到菜单项,它会自动显示该命令名称旁边的手势而无需任何额外efforr,只是:

<MenuItem Command="ApplicationCommands.New" /> 

这将显示[新建Ctrl + N]菜单项。

+0

感谢您的回复,但我不认为它回答了我问的问题。我想要做的就是在按钮上放一个Ctrl键;例如,Ctrl-O来调用“打开文件”按钮。 – 2009-09-11 21:35:27

+1

+0

+1 Carlo,我会将您的评论转移到答案中,因为您的评论确实是答案。 :) – RichardOD 2010-12-11 15:12:29

0

最简单和肮脏的方法是自己添加键盘快捷键。当然,如果您更改了在Command中使用的快捷方式,则必须在工具提示中手动更改它。但是,也许我们可以将工具提示文本绑定到Command的属性以通过绑定获取快捷方式?任何人都知道可能吗?

 <Button Command="{Binding OpenDllCommand}" Content="Bla-bla > 
      <Button.ToolTip> 
       <StackPanel> 
        <TextBlock Text="F6" FontWeight="Bold" /> 
        <TextBlock Text="Open MbUnit/MINT DLL with tests" /> 
       </StackPanel> 
      </Button.ToolTip> 
     </Button>