已经讨论了十几个或更多有关此主题的SO问题和文章。似乎没什么用。这是我的简单系统。主Window
,其中包含Frame
个主机Page
个对象。我的一个Page
对象中包含以下内容:
<Page.Resources>
<RoutedUICommand x:Key="PreviousRecordCommand" Text="Previous Record" />
<RoutedUICommand x:Key="NextRecordCommand" Text="Previous Record" />
<CollectionViewSource x:Key="cvsAllOrders" Source="{Binding AllSalesOrders}" />
</Page.Resources>
<Page.CommandBindings>
<CommandBinding Command="{StaticResource PreviousRecordCommand}" Executed="PreviousRecordCommand_Executed" CanExecute="PreviousRecordCommandBinding_CanExecute" />
<CommandBinding Command="{StaticResource NextRecordCommand}" Executed="NextRecordCommand_Executed" CanExecute="NextRecordCommandBinding_CanExecute" />
</Page.CommandBindings>
<Page.InputBindings>
<KeyBinding Command="{StaticResource PreviousRecordCommand}" Gesture="CTRL+LEFT" />
<KeyBinding Command="{StaticResource NextRecordCommand}" Gesture="CTRL+RIGHT" />
</Page.InputBindings>
代码隐藏有:
Private Sub PreviousRecordCommand_Executed(sender As System.Object, e As System.Windows.Input.ExecutedRoutedEventArgs)
Dim CurView = DirectCast(Me.Resources("cvsAllOrders"), CollectionViewSource).View
CurView.MoveCurrentToPrevious()
If CurView.IsCurrentBeforeFirst Then CurView.MoveCurrentToFirst()
End Sub
Private Sub NextRecordCommand_Executed(sender As System.Object, e As System.Windows.Input.ExecutedRoutedEventArgs)
Dim CurView = DirectCast(Me.Resources("cvsAllOrders"), CollectionViewSource).View
CurView.MoveCurrentToNext()
If CurView.IsCurrentAfterLast Then CurView.MoveCurrentToLast()
End Sub
Private Sub PreviousRecordCommandBinding_CanExecute(sender As System.Object, e As System.Windows.Input.CanExecuteRoutedEventArgs)
Dim MyView = DirectCast(Me.Resources("cvsAllOrders"), CollectionViewSource).View
e.CanExecute = (MyView IsNot Nothing) AndAlso (DirectCast(Me.Resources("cvsAllOrders"), CollectionViewSource).View.CurrentPosition > 0)
End Sub
Private Sub NextRecordCommandBinding_CanExecute(sender As System.Object, e As System.Windows.Input.CanExecuteRoutedEventArgs)
Dim MyView = DirectCast(Me.Resources("cvsAllOrders"), CollectionViewSource).View
e.CanExecute = (MyView IsNot Nothing) AndAlso (DirectCast(Me.Resources("cvsAllOrders"), CollectionViewSource).View.CurrentPosition < DirectCast(Me.Resources("cvsAllOrders"), CollectionViewSource).View.Cast(Of SalesOrderEntryModel.SalesOrder).Count() - 1)
End Sub
我无法让这个工作。我也试过在我的代码隐藏中声明命令。没有任何区别。作为参考,我在Page
上有几十个文本框和其他控件,需要这些键才能在当前焦点的任何位置工作。
非常感谢任何帮助。