2016-11-08 53 views
1

我無法從後面的代碼中設置InputGestureText。 快捷鍵可以正常工作,但鍵串不會與菜單項一起顯示。無法在後面的代碼中設置InputGestureText

下面提到了XAML和C#代碼。

環境: VS2012 & .NET框架4.5

任何想法,我缺少的是什麼?

//******************** 
//XAML 
//******************** 
     <DockPanel Grid.Row="0" Grid.ColumnSpan="2"> 
      <Menu DockPanel.Dock="Top" Background="White"> 
       <MenuItem x:Name="mnuFile" Header="_File"> 
        <MenuItem x:Name="mnuFileNew" Header="_New..." /> 
        <MenuItem x:Name="mnuFileOpen" Header="_Open for Editing..." /> 
        <Separator /> 
        <MenuItem x:Name="mnuFileExit" Header="E_xit" /> 
       </MenuItem> 
      </Menu> 
     </DockPanel> 
//******************** 


//******************** 
//Code Behind 
//******************** 

public MainWindow() 
{ 
    InitializeComponent(); 

    //File > New 
    mnuFileNew.Click += mnuFileNew_Click; 
    RoutedCommand cmdNewReport = new RoutedCommand(); 
    cmdNewReport.InputGestures.Add(new KeyGesture(Key.N, ModifierKeys.Control | ModifierKeys.Shift, "Ctrl+Shift+N")); 
    CommandBindings.Add(new CommandBinding(cmdNewReport, mnuFileNew_Click)); 
} 

void mnuFileNew_Click(object sender, RoutedEventArgs e) 
{ 
    MessageBox.Show("New Report not implemented!", this.Title, MessageBoxButton.OK, MessageBoxImage.Information); 
} 
//******************** 

回答

0

如果你想背後使用代碼,然後嘗試使用此顯示快捷:

mnuFileNew.Click += mnuFileNew_Click; 
RoutedCommand cmdNewReport = new RoutedCommand(); 

var keyGesture = new KeyGesture(Key.N, ModifierKeys.Control | ModifierKeys.Shift, "Ctrl+Shift+N"); 
cmdNewReport.InputGestures.Add(keyGesture); 
mnuFileNew.InputGestureText = keyGesture.DisplayString; 

CommandBindings.Add(new CommandBinding(cmdNewReport, mnuFileNew_Click)); 
+0

和它的作品!謝謝。 – Ashish

相關問題