private NavigationViewItem GetParentNavigationViewItemForContainer(NavigationViewItemBase nvib)
        {
            //TODO: This scenario does not find parent items when in a flyout, which causes problems if item if first loaded
            //straight in the flyout. Fix.This logic can be merged with the 'GetIndexPathForContainer' logic below.
            var parent = GetParentItemsRepeaterForContainer(nvib);

            if (!IsRootItemsRepeater(parent))
            {
                return(parent.FindAncestorOfType <NavigationViewItem>());
            }

            return(null);
        }
        private ItemsRepeater GetParentRootItemsRepeaterForContainer(NavigationViewItemBase nvib)
        {
            var parentIR = GetParentItemsRepeaterForContainer(nvib);

            while (!IsRootItemsRepeater(parentIR))
            {
                nvib = GetParentNavigationViewItemForContainer(nvib);
                if (nvib == null)
                {
                    return(null);
                }

                parentIR = GetParentItemsRepeaterForContainer(nvib);
            }

            return(parentIR);
        }
        private bool IsContainerTheSelectedItemInTheSelectionModel(NavigationViewItemBase nvib)
        {
            var selItem = _selectionModel.SelectedItem;

            if (selItem == null)
            {
                return(false);
            }

            var selItemCont = selItem as NavigationViewItemBase;

            if (selItemCont == null)
            {
                selItemCont = GetContainerForIndexPath(_selectionModel.SelectedIndex);
            }

            return(selItemCont == nvib);
        }
        private IndexPath GetIndexPathForContainer(NavigationViewItemBase nvib)
        {
            var  path           = new List <int>(4);
            bool isInFooterMenu = false;

            IControl child  = nvib;
            var      parent = nvib.GetVisualParent();

            if (parent == null)
            {
                return(IndexPath.CreateFromIndices(path));
            }

            // Search through VisualTree for a root ItemsRepeater
            while (parent != null && !IsRootItemsRepeater(parent) && !IsRootGridOfFlyout(parent))
            {
                if (parent is ItemsRepeater ir)
                {
                    path.Insert(0, ir.GetElementIndex(child));
                }
                child  = (IControl)parent;
                parent = parent.GetVisualParent();
            }

            // If the item is in a flyout, then we need to final index of its parent
            if (IsRootGridOfFlyout(parent))
            {
                if (_lastItemExpandedIntoFlyout != null)
                {
                    child  = _lastItemExpandedIntoFlyout;
                    parent = IsTopNavigationView ? _topNavRepeater : _leftNavRepeater;
                }
            }

            // If item is in one of the disconnected ItemRepeaters, account for that in IndexPath calculations
            if (parent == _topNavRepeaterOverflowView)
            {
                // Convert index of selected item in overflow to index in datasource
                var contIndex   = _topNavRepeaterOverflowView.GetElementIndex(child);
                var item        = _topDataProvider.GetOverflowItems()[contIndex];
                var indexAtRoot = _topDataProvider.IndexOf(item);
                path.Insert(0, indexAtRoot);
            }
            else if (parent == _topNavRepeater)
            {
                // Convert index of selected item in overflow to index in datasource
                var contIndex   = _topNavRepeater.GetElementIndex(child);
                var item        = _topDataProvider.GetPrimaryItems()[contIndex];
                var indexAtRoot = _topDataProvider.IndexOf(item);
                path.Insert(0, indexAtRoot);
            }
            else if (parent is ItemsRepeater parentIR)
            {
                path.Insert(0, parentIR.GetElementIndex(child));
            }

            isInFooterMenu = parent == _leftNavFooterMenuRepeater || parent == _topNavFooterMenuRepeater;

            path.Insert(0, isInFooterMenu ? _footerMenuBlockIndex : _mainMenuBlockIndex);

            return(IndexPath.CreateFromIndices(path));
        }
 private ItemsRepeater GetParentItemsRepeaterForContainer(NavigationViewItemBase nvib)
 {
     return(nvib?.FindAncestorOfType <ItemsRepeater>());
 }
 private bool IsTopLevelItem(NavigationViewItemBase nvib)
 {
     return(IsRootItemsRepeater(GetParentItemsRepeaterForContainer(nvib)));
 }