/// <summary> /// Sets focus to the next item in the ContextMenu. /// </summary> /// <param name="down">True to move the focus down; false to move it up.</param> private void FocusNextItem(bool down) { int count = Items.Count; int startingIndex = down ? -1 : count; MenuItem focusedMenuItem = FocusManager.GetFocusedElement() as MenuItem; if (null != focusedMenuItem && (this == focusedMenuItem.ParentMenuBase)) { startingIndex = ItemContainerGenerator.IndexFromContainer(focusedMenuItem); } int index = startingIndex; do { index = (index + count + (down ? 1 : -1)) % count; MenuItem container = ItemContainerGenerator.ContainerFromIndex(index) as MenuItem; if (null != container) { if (container.IsEnabled && container.Focus()) { break; } } }while (index != startingIndex); }
private bool OnEnterMenuMode(object sender, EventArgs e) { // Don't enter menu mode if someone has capture if (Mouse.Captured != null) { return(false); } // Need to check that ALT/F10 happened in our source. PresentationSource source = sender as PresentationSource; PresentationSource mySource = null; mySource = PresentationSource.CriticalFromVisual(this); if (source == mySource) { // Give focus to the first possible element in the ItemsControl for (int i = 0; i < Items.Count; i++) { MenuItem menuItem = ItemContainerGenerator.ContainerFromIndex(i) as MenuItem; if (menuItem != null && !(Items[i] is Separator)) { if (menuItem.Focus()) { return(true); } } } } return(false); }
private bool OnEnterMenuMode(object sender, EventArgs e) { if (Mouse.Captured != null) { return(false); } PresentationSource presentationSource = sender as PresentationSource; PresentationSource presentationSource2 = PresentationSource.CriticalFromVisual(this); if (presentationSource == presentationSource2) { for (int i = 0; i < base.Items.Count; i++) { MenuItem menuItem = base.ItemContainerGenerator.ContainerFromIndex(i) as MenuItem; if (menuItem != null && !(base.Items[i] is Separator) && menuItem.Focus()) { return(true); } } } return(false); }
static void EmulateClick(MenuItem item, WPFContextMenu.Clean clean) { IInvokeProvider invoker = new MenuItemAutomationPeer(item); item.Focus(); invoker.Invoke(); InvokeUtility.DoEvents(); clean(); }