void OnKeyDown(object sender, KeyRoutedEventArgs e) { if (SelectionMode == SelectionMode.Multiple || RootItems.Count == 0) { return; } TvItem row; switch (e.Key) { case VirtualKey.Down: row = SelectedRows.LastOrDefault(); if (row == null) { OnToggleSelected(RootItems[0]); } else { using (var ls = RootItems.GetExpandedItems().GetEnumerator()) { while (ls.MoveNext()) { if (ls.Current == row) { if (ls.MoveNext()) { OnToggleSelected(ls.Current); } else { OnToggleSelected(RootItems[0]); } break; } } } } e.Handled = true; return; case VirtualKey.Up: row = SelectedRows.LastOrDefault(); if (row == null) { OnToggleSelected(RootItems[0]); } else { using (var ls = RootItems.GetExpandedItems().GetEnumerator()) { if (ls.MoveNext()) { if (ls.Current == row) { SelectedItem = null; } else { while (true) { TvItem lastRow = ls.Current; if (ls.MoveNext()) { if (ls.Current == row) { OnToggleSelected(lastRow); break; } } } } } } } e.Handled = true; return; case VirtualKey.Enter: row = SelectedRows.LastOrDefault(); if (row != null) { row.OnClick(); } return; default: return; } }