/// <summary>
        /// Build the list of traces for the overflow button.
        /// </summary>
        private void BuildTraces()
        {
            BreadcrumbItem item = RootItem;

            traces.Clear();
            if (item != null && item.IsOverflow)
            {
                foreach (object trace in item.Items)
                {
                    MenuItem menuItem = new MenuItem();//new RibbonMenuItem();
                    menuItem.Tag = trace;
                    BreadcrumbItem bcItem = item.ContainerFromItem(trace);

                    menuItem.Click += menuItem_Click;
                    menuItem.Icon   = GetImage(bcItem != null ? bcItem.Image : null);
                    if (trace == RootItem.SelectedItem)
                    {
                        menuItem.FontWeight = FontWeight.Bold;
                    }
                    traces.Add(menuItem);
                    if (bcItem != null)
                    {
                        menuItem.Header = bcItem.TraceValue;
                    }
                }
                traces.Insert(0, new Separator());
                MenuItem rootMenuItem = new MenuItem();//new RibbonMenuItem();
                rootMenuItem.Header           = item.TraceValue;
                rootMenuItem.Command          = SelectRootCommand;
                rootMenuItem.CommandParameter = item;
                rootMenuItem.Icon             = GetImage(item.Image);
                traces.Insert(0, rootMenuItem);
            }

            item = item != null ? item.SelectedBreadcrumb : null;

            while (item != null)
            {
                if (!item.IsOverflow)
                {
                    break;
                }
                MenuItem traceMenuItem = new MenuItem();
                traceMenuItem.Header           = item.TraceValue;
                traceMenuItem.Command          = SelectRootCommand;
                traceMenuItem.CommandParameter = item;
                traceMenuItem.Icon             = GetImage(item.Image);
                traces.Insert(0, traceMenuItem);
                item = item.SelectedBreadcrumb;
            }
        }
Exemple #2
0
        private MenuItem CreateMenuItem(object item)
        {
            //RibbonMenuItem menuItem = new RibbonMenuItem();
            MenuItem menuItem = new MenuItem();

            menuItem.DataContext = item;
            BreadcrumbItem bi = item as BreadcrumbItem;

            if (bi == null)
            {
                BreadcrumbItem parent = TemplatedParent as BreadcrumbItem;
                if (parent != null)
                {
                    bi = parent.ContainerFromItem(item);
                }
            }
            if (bi != null)
            {
                menuItem.Header = bi.TraceValue;
            }

            Image image = new Image();

            image.Source = bi != null ? bi.Image : null;
            //image.SnapsToDevicePixels = true;
            image.Stretch             = Stretch.Fill;
            image.VerticalAlignment   = Layout.VerticalAlignment.Center;
            image.HorizontalAlignment = Layout.HorizontalAlignment.Center;
            image.Width  = 16;
            image.Height = 16;

            menuItem.Icon = image;

            menuItem.Click += item_Click;// += new RoutedEventHandler(item_Click);
            if (item != null && item.Equals(SelectedItem))
            {
                menuItem.FontWeight = FontWeight.Bold;
            }
            menuItem.ItemTemplate = ItemTemplate;
            //menuItem.ItemTemplateSelector = ItemTemplateSelector;
            //contextMenu.Items.OfType<object>().ToList().Add(menuItem);
            return(menuItem);
        }
        /// <summary>
        /// Traces the specified path and builds the associated BreadcrumbItems.
        /// </summary>
        /// <param name="path">The traces separated by the SepearatorString property.</param>
        private bool BuildBreadcrumbsFromPath(string newPath)
        {
            PathConversionEventArgs e = new PathConversionEventArgs(ConversionMode.EditToDisplay, newPath, Root, PathConversionEvent);

            RaiseEvent(e);
            newPath = e.DisplayPath;

            BreadcrumbItem item = RootItem;

            if (item == null)
            {
                this.Path = null;
                return(false);
            }

            newPath = RemoveLastEmptySeparator(newPath);
            string[] traces = newPath.Split(new string[] { SeparatorString }, StringSplitOptions.RemoveEmptyEntries);
            if (traces.Length == 0)
            {
                RootItem.SelectedItem = null;
            }
            int index = 0;

            List <int> itemIndex = new List <int>();

            // if the root is specified as first trace, then skip:
            int length = traces.Length;
            int max    = BreadcrumbsToHide;

            if (traces.Length > index && max > 0 && traces[index] == (RootItem.TraceValue))
            {
                length--;
                index++;
                max--;
            }

            for (int i = index; i < traces.Length; i++)
            {
                if (item == null)
                {
                    break;
                }

                string trace = traces[i];
                OnPopulateItems(item);
                object next = item.GetTraceItem(trace);
                if (next == null)
                {
                    break;
                }
                itemIndex.Add(item.Items.OfType <object>().ToList().IndexOf(next));
                BreadcrumbItem container = item.ContainerFromItem(next);

                item = container;
            }
            if (length != itemIndex.Count)
            {
                //recover the last path:
                Path = GetDisplayPath();
                return(false);
            }

            // temporarily remove the SelectionChangedEvent handler to minimize processing of events while building the breadcrumb items:
            RemoveHandler(BreadcrumbItem.SelectionChangedEvent, breadcrumbItemSelectedItemChanged);
            try
            {
                item = RootItem;
                for (int i = 0; i < itemIndex.Count; i++)
                {
                    if (item == null)
                    {
                        break;
                    }
                    item.SelectedIndex = itemIndex[i];
                    item = item.SelectedBreadcrumb;
                }
                if (item != null)
                {
                    item.SelectedItem = null;
                }
                SelectedBreadcrumb = item;
                SelectedItem       = item != null ? item.Data : null;
            }
            finally
            {
                AddHandler(BreadcrumbItem.SelectionChangedEvent, breadcrumbItemSelectedItemChanged);
            }

            return(true);
        }