2010-08-02 35 views
1

我知道「_」而不是「&」,但是如何將F1,F2 ...鍵分配給按鈕? 這是一個小的應用程序,我沒有使用命令只是直click事件處理程序,但如果有必要WPF - 將F鍵指定給按鈕

回答

4

我可以使用命令在小項目,現在我的工作,我有這樣的:

<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(); } 
+0

太多的代碼恕我直言;我最終創建了一個帶有「Key」屬性的自定義按鈕類,在PreviewKeyUp上,我正在搜索右鍵屬性 – 2010-08-02 11:43:55

6

容易吧...... 試試看吧....跟着我

在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"/> 

在CS中的文件:

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

的第一個子按鈕太簡單了。你應該回答更多問題! – 2016-04-24 23:01:55

+0

點擊按鈕時,'btn_font_Click'執行兩次。爲避免這種情況,'btn_font_Click'不應出現在'