C# 如何使用箭头键在Listview中从一个文本框转到另一个文本框?

C# 如何使用箭头键在Listview中从一个文本框转到另一个文本框?,c#,wpf,listview,C#,Wpf,Listview,我有一个通过itemtemplate显示的列表视图。itemtemplate包含一个文本框,所以如果有多行出现,那么我希望通过箭头键聚焦文本框。 现在我用这个代码 private void txtBillRate_PreviewKeyDown(object sender, KeyEventArgs e) { try { if (e.Key == Key.Up|| e.Key==Key.Down) {

我有一个通过itemtemplate显示的列表视图。itemtemplate包含一个文本框,所以如果有多行出现,那么我希望通过箭头键聚焦文本框。 现在我用这个代码

    private void txtBillRate_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            if (e.Key == Key.Up|| e.Key==Key.Down)
            {
                MoveToNextUIElement(e);                   
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    void MoveToNextUIElement(KeyEventArgs e)
    {
        // Creating a FocusNavigationDirection object and setting it to a
        // local field that contains the direction selected.
        FocusNavigationDirection focusDirection = FocusNavigationDirection.Next;

        // MoveFocus takes a TraveralReqest as its argument.
        TraversalRequest request = new TraversalRequest(focusDirection);

        // Gets the element with keyboard focus.
        UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;

        // Change keyboard focus.
        if (elementWithFocus != null)
        {
            if (elementWithFocus.MoveFocus(request)) e.Handled = true;
        }
    }
使用此代码可以从listview中释放焦点