internal void FocusItem(ListBoxItem item) { if (item != null) { focusedItem = item; if (!item.IsFocused) item.Focus (); } }
internal virtual void NotifyListItemLoaded (ListBoxItem listBoxItem) { if (ItemContainerGenerator.ItemFromContainer (listBoxItem) == SelectedItem) { listBoxItem.IsSelected = true; listBoxItem.Focus (); } }
private void MakeSingleSelection(ListBoxItem listItem) { if (ItemsControlFromItemContainer(listItem) == this) { ItemInfo info = ItemInfoFromContainer(listItem); SelectionChange.SelectJustThisItem(info, true /* assumeInItemsCollection */); listItem.Focus(); UpdateAnchorAndActionItem(info); } }
public void Focusable () { bool loaded = false; ListBoxItem item = new ListBoxItem { Content = "Hello World" }; item.Loaded += delegate { loaded = true; }; ListBox box = new ListBox (); box.Items.Add (item); TestPanel.Children.Add (box); EnqueueConditional (() => loaded); Enqueue (() => { Assert.IsTrue (item.Focus (), "#1"); box.SelectedItem = item; box.SelectedItem = null; Assert.IsTrue (item.Focus (), "#2"); }); EnqueueTestComplete (); }
protected override void OnKeyDown(KeyEventArgs e) { if (!e.Handled) { bool handled = false; int newFocusedIndex = -1; switch (e.Key) { case Key.Space: case Key.Enter: if ((Key.Enter != e.Key) || KeyboardNavigation.GetAcceptsReturn(this)) { if (ModifierKeys.Alt != (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Alt))) { // KeyEventArgs.OriginalSource (used by WPF) isn't available in Silverlight; use FocusManager.GetFocusedElement instead ListBoxItem listBoxItem = FocusManager.GetFocusedElement() as ListBoxItem; if (null != listBoxItem) { if ((ModifierKeys.Control == (Keyboard.Modifiers & ModifierKeys.Control)) && listBoxItem.IsSelected) { SelectedItem = null; } else { SelectedItem = ItemContainerGenerator.ItemFromContainer(listBoxItem); } handled = true; } } } break; case Key.Home: newFocusedIndex = 0; break; case Key.End: newFocusedIndex = Items.Count - 1; break; case Key.PageUp: newFocusedIndex = NavigateByPage(false); break; case Key.PageDown: newFocusedIndex = NavigateByPage(true); break; case Key.Left: if (IsVerticalOrientation()) { ElementScrollViewerScrollInDirection(Key.Left); } else { newFocusedIndex = _focusedIndex - 1; } break; case Key.Up: if (IsVerticalOrientation()) { newFocusedIndex = _focusedIndex - 1; } else { ElementScrollViewerScrollInDirection(Key.Up); } break; case Key.Right: if (IsVerticalOrientation()) { ElementScrollViewerScrollInDirection(Key.Right); } else { newFocusedIndex = _focusedIndex + 1; } break; case Key.Down: if (IsVerticalOrientation()) { newFocusedIndex = _focusedIndex + 1; } else { ElementScrollViewerScrollInDirection(Key.Down); } break; // case Key.Divide: // Feature only used by SelectionMode.Extended // case Key.Oem2: // Key not supported by Silverlight // break; // case Key.Oem5: // Key not supported by Silverlight // break; default: Debug.Assert(!handled); break; } if ((-1 != newFocusedIndex) && (-1 != _focusedIndex) && (newFocusedIndex != _focusedIndex) && (0 <= newFocusedIndex) && (newFocusedIndex < Items.Count)) { // A key press will change the focused ListBoxItem ListBoxItem listBoxItem = (ListBoxItem)ItemContainerGenerator.ContainerFromIndex(newFocusedIndex); Debug.Assert(null != listBoxItem); ScrollIntoView(ItemContainerGenerator.ItemFromContainer(listBoxItem)); if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) { listBoxItem.Focus(); } else { SelectedItem = ItemContainerGenerator.ItemFromContainer(listBoxItem); } handled = true; } if (handled) { e.Handled = true; } } }
private void MakeSingleSelection(ListBoxItem listItem) { if (ItemsControlFromItemContainer(listItem) == this) { object item = ItemContainerGenerator.ItemFromContainer(listItem); SelectionChange.SelectJustThisItem(item, true /* assumeInItemsCollection */); listItem.Focus(); UpdateAnchorAndActionItem(listItem); } }