IndexFromContainer() public method

public IndexFromContainer ( DependencyObject container ) : int
container DependencyObject
return int
        /// <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);
        }
Esempio n. 2
0
        /// <summary>
        /// This is the method that responds to the KeyDown event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            TabItem nextTabItem = null;

            // Handle [Ctrl][Shift]Tab, Home and End cases
            // We have special handling here because if focus is inside the TabItem content we cannot
            // cycle through TabItem because the content is not part of the TabItem visual tree

            int direction  = 0;
            int startIndex = -1;

            switch (e.Key)
            {
            case Key.Tab:
                if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                {
                    startIndex = ItemContainerGenerator.IndexFromContainer(ItemContainerGenerator.ContainerFromItem(SelectedItem));
                    if ((e.KeyboardDevice.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                    {
                        direction = -1;
                    }
                    else
                    {
                        direction = 1;
                    }
                }
                break;

            case Key.Home:
                direction  = 1;
                startIndex = -1;
                break;

            case Key.End:
                direction  = -1;
                startIndex = Items.Count;
                break;
            }

            nextTabItem = FindNextTabItem(startIndex, direction);

            if (nextTabItem != null && nextTabItem != SelectedItem)
            {
                e.Handled = nextTabItem.SetFocus();
            }

            if (!e.Handled)
            {
                base.OnKeyDown(e);
            }
        }
Esempio n. 3
0
        private void SetRangeSelection(ListBoxItem item)
        {
            int itemIndex            = ItemContainerGenerator.IndexFromContainer(item);
            int selectionAnchorIndex = ItemContainerGenerator.IndexFromContainer(selectionAnchor);

            int rangeStartIndex = itemIndex.Min(selectionAnchorIndex);
            int rangeEndIndex   = itemIndex.Max(selectionAnchorIndex);

            for (int i = 0; i < ItemContainerGenerator.ItemsCount; i++)
            {
                DependencyObject itemContainer = ItemContainerGenerator.Generate(i);
                itemContainer.SetCurrentValue(Selector.IsSelectedProperty, rangeStartIndex <= i && i <= rangeEndIndex);
            }
        }
Esempio n. 4
0
 private int ElementIndex(ListBoxItem listItem)
 {
     return(ItemContainerGenerator.IndexFromContainer(listItem));
 }
Esempio n. 5
0
 /// <summary>
 /// Called by ListBoxItem instances when they get focus
 /// </summary>
 /// <param name="listBoxItemNewFocus">ListBoxItem that got focus</param>
 internal override void NotifyListItemGotFocus(ListBoxItem listBoxItemNewFocus)
 {
     // Track the focused index
     _focusedIndex = ItemContainerGenerator.IndexFromContainer(listBoxItemNewFocus);
 }
Esempio n. 6
0
            // update container and index with current values
            internal ItemInfo Refresh(ItemContainerGenerator generator)
            {
                if (Container == null && Index < 0)
                {
                    Container = generator.ContainerFromItem(Item);
                }

                if (Index < 0 && Container != null)
                {
                    Index = generator.IndexFromContainer(Container);
                }

                if (Container == null && Index >= 0)
                {
                    Container = generator.ContainerFromIndex(Index);
                }

                if (Container == SentinelContainer && Index >= 0)
                {
                    Container = null;   // caller explicitly wants null container
                }

                return this;
            }