我有一个listview,通过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中失去焦点。