代码之家  ›  专栏  ›  技术社区  ›  Catalin DICU

WPF-为按钮指定F键

  •  1
  • Catalin DICU  · 技术社区  · 15 年前

    我知道的是“\”而不是“&”但是如何分配F1,F2。。。按钮的钥匙? 这是一个小应用程序,我不使用命令,只是直接单击事件处理程序,但如果需要,我可以使用命令

    2 回复  |  直到 15 年前
        1
  •  6
  •   snurre    15 年前

    在我现在从事的一个小项目中,我有:

    <Window.Resources>
        <c:CommandReference x:Key="ExitCommandReference" Command="{Binding ExitCommand}" />
        <c:CommandReference x:Key="ReloadCommandReference" Command="{Binding ReloadCommand}" />
    </Window.Resources>
    <Window.InputBindings>
        <KeyBinding Key="F4" Modifiers="Alt" Command="{StaticResource ExitCommandReference}" />
        <KeyBinding Key="F5" Command="{StaticResource ReloadCommandReference}"/>
    </Window.InputBindings>
    

    在代码中:

    private DelegateCommand exitCommand;
    private DelegateCommand reloadCommand;
    public ICommand ExitCommand { get { return exitCommand ?? (exitCommand = new DelegateCommand(Exit)); } }
    public ICommand ReloadCommand { get { return reloadCommand ?? (reloadCommand = new DelegateCommand(Reload)); } }
    private static void Exit() { Application.Current.Shutdown(); }
    private void Reload() { LoadData(); }
    
        2
  •  7
  •   pcking60    12 年前

    穿上试试……跟我来

    在Xaml文件中:

        <Window.Resources>
            <RoutedUICommand x:Key="cmd1"></RoutedUICommand>
        </Window.Resources>
        <Window.CommandBindings>
            <CommandBinding Command="{StaticResource cmd1}" Executed="btn_font_Click"> </CommandBinding>
        </Window.CommandBindings>
        <Window.InputBindings>
            <KeyBinding  Key="F1" Command="{StaticResource cmd1}"></KeyBinding>
        </Window.InputBindings>
        <Button  Command="{StaticResource cmd1}" Name="btn_font" Grid.Column="2" Grid.Row="1" Width="60" Height="25" Content="Enter" Click="btn_font_Click" Margin="10,2"/>
    

    private void btn_font_Click(object sender, RoutedEventArgs e)
        {
            Font_Chooser fc = new Font_Chooser();
            fc.ShowDialog();
        }