按Enter键两次时的命令执行

时间:2016-06-01 05:57:59

标签: c# wpf xaml combobox prism

我有一个组合框。我有一些输入绑定如下:

<ComboBox .........>

    <ComboBox.InputBindings>
        <KeyBinding Command="{Binding DataContext.DeleteUnwantedOrderItemTransactionCommand, 
                                      RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type coreUI:UserControlViewBase}}}"
                    Gesture="Return" />
        <KeyBinding Command="{Binding DataContext.DeleteUnwantedOrderItemTransactionCommand, 
                                      RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type coreUI:UserControlViewBase}}}"
                    Gesture="Tab" />
    </ComboBox.InputBindings>

</ComboBox>

在ViewModel中,我有RelayCommands,如下所示:

public RelayCommand DeleteUnwantedOrderItemTransactionCommand { get; set; }
public RelayCommand AddNewOrderItemTransactionCommand { get; set; }

public OrderViewModel(IEventAggregator _eventAggregator)
{
    eventAggregator = _eventAggregator;

    DeleteUnwantedOrderItemTransactionCommand = new RelayCommand(DeleteUnwantedOrderItemTransaction);
    AddNewOrderItemTransactionCommand = new RelayCommand(AddNewOrderItemTransaction);
}

protected void DeleteUnwantedOrderItemTransaction(object obj)
{
    if (!(SelectedOrderItemTransaction.ItemId > 0))
    {
        NewOrder.OrderItemTransactions.Remove(SelectedOrderItemTransaction);
    }

    if (NewOrder.OrderItemTransactions.Count == 0)
    {
        NewOrder.OrderItemTransactions.Add(new OrderItemTransaction());
    }

    eventAggregator.GetEvent<ChangeFocusToNextUIElementEvent>().Publish(true);
}

protected void AddNewOrderItemTransaction(object obj)
{
    if (SelectedOrderItemTransaction == NewOrder.OrderItemTransactions.Last())
        NewOrder.OrderItemTransactions.Add(new OrderItemTransaction());

    eventAggregator.GetEvent<ChangeFocusToNextUIElementEvent>().Publish(true);
}

然后在CodeBehind:

public OrderView(OrderViewModel _viewModel, IEventAggregator _eventAggregator)
{
    InitializeComponent();
    this.DataContext = _viewModel;
    _eventAggregator.GetEvent<ChangeFocusToNextUIElementEvent>().Subscribe(MoveToNextUIElement);
}

void MoveToNextUIElement(bool obj)
{
    // Gets the element with keyboard focus.
    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;

    if (elementWithFocus != null)
    {
        elementWithFocus.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    }
}

问题:

我在InputBindings中指定当用户按 Enter Tab 时,我想执行一个RelayCommand并在执行完该Command时,我想移动焦点到下一个元素。

在这种情况下:

当我按 Tab 时,一切正常。执行命令并将焦点移至下一个元素。

但是当我按 Enter 1 st 时,会在ComboBox中选择该项。命令不会触发,因此,焦点不会移动到下一个元素。当我按 Enter 2 nd 时,Command执行并且焦点按预期移动到下一个Control。

但我不想要这种行为。当按下 st 时间 Enter 时,我想执行命令并将焦点移动到下一个元素。

1 个答案:

答案 0 :(得分:1)

只看了ComboBox的源代码。

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
    // Let ComboBox's original method handle it
    base.OnPreviewKeyDown(e);

    // Manually check if the ComboBox handled an "Enter" key
    // If yes, set e.Handled back to false so that Command binding can consume the same event
    Key key = e.Key;
    if (key == Key.Enter)
        e.Handled = false;
}

只是说 - 我也没有测试过这个!但我认为它应该有效。如果没有尝试也为OnKeyDown()执行此操作。显然,ComboBox有两种方法,都调用私有方法KeyDownHandler(KeyEventArgs e),我们无法覆盖它。