2

已经阅读了十几个关于这个主题的 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,需要这些键在当前焦点所在的任何地方工作。

任何帮助将不胜感激。

4

0 回答 0