public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            buttonExpand = (ToggleButton)GetTemplateChild("buttonExpand");
            button       = (Button)GetTemplateChild("button");

            menu = (ContextMenu)GetTemplateChild("menu");
            if (menu != null)
            {
                menu.DataContext = DataContext;
                menu.Opened     += menu_Opened;

                BreadcrumbItem templatedParent = TemplatedParent as BreadcrumbItem;
                if (templatedParent != null && templatedParent.Items.Count > 0)
                {
                    DataTemplateSelector selector = VisualTreHelperEx.GetDefaultSelector();
                    DataTemplate         template = selector.SelectTemplate(templatedParent.Items[0], this);
                    menu.ItemTemplate = template;
                }

                menu.CommandBindings.Add(new CommandBinding(InvokeMenuCommand, CommandBindingToMenuItem_Executed));
            }
            DependencyPropertyDescriptor isPressedProperty = DependencyPropertyDescriptor.FromProperty(IsPressedProperty, typeof(Button));

            isPressedProperty.AddValueChanged(button, OnIsPressedChangedIntern);
        }
        private void CommandBindingToButton_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            FrameworkElement itemsControl = DataContext as FrameworkElement;

            if (itemsControl != null)
            {
                Breadcrumb parent = VisualTreHelperEx.FindVisualParent <Breadcrumb>(this);
                object     target = itemsControl.DataContext;
                if (target == null)
                {
                    target = itemsControl;
                }
                parent.GoTo(target);
            }
        }
Exemple #3
0
        private void CommandBindingToMenuItem_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var breadcrumb = VisualTreHelperEx.FindVisualParent <Breadcrumb>(this);

            breadcrumb.AddTrail(ParentItem, e.Parameter);
        }
        void menu_Opened(object sender, RoutedEventArgs e)
        {
            BreadcrumbView view = View;

            BreadcrumbItem templatedParent = TemplatedParent as BreadcrumbItem;

            menu.Placement        = PlacementMode.Relative;
            menu.PlacementTarget  = buttonExpand;
            menu.VerticalOffset   = buttonExpand.ActualHeight;
            menu.HorizontalOffset = buttonExpand.ActualWidth + menu.BorderThickness.Left;

            if ((templatedParent != null) && templatedParent.IsFirst)
            {
                Image image = VisualTreHelperEx.FindVisualChild <Image>(templatedParent);
                if (image != null)
                {
                    menu.HorizontalOffset -= image.ActualWidth;
                }
            }

            if (view != null)
            {
                int index = view.ItemContainerGenerator.IndexFromContainer(TemplatedParent);
                if (index >= 0)
                {
                    menu.UpdateLayout();
                    TextBlock menuText = VisualTreHelperEx.FindVisualChild <TextBlock>(menu);
                    if (menuText != null)
                    {
                        double    menuTextOffset = menuText.TransformToVisual(menu).Transform(new Point(0, 0)).X;
                        TextBlock buttonText     = VisualTreHelperEx.FindVisualChild <TextBlock>(button);
                        if (buttonText != null)
                        {
                            double buttonTextOffset = buttonText.TransformToVisual((Visual)TemplatedParent).Transform(new Point(0, 0)).X;
                            menu.HorizontalOffset -= (menuTextOffset - buttonTextOffset);
                        }
                    }
                }
            }


            for (var index = 0; index < menu.Items.Count; index++)
            {
                object   o        = menu.Items[index];
                MenuItem menuItem = menu.ItemContainerGenerator.ContainerFromIndex(index) as MenuItem;
                if (menuItem != null)
                {
                    FieldInfo rolePropertyKeyField = typeof(MenuItem).GetField("RolePropertyKey",
                                                                               BindingFlags.NonPublic | BindingFlags.Static);
                    DependencyPropertyKey rolePropertyKey = (DependencyPropertyKey)rolePropertyKeyField.GetValue(menuItem);
                    menuItem.SetValue(rolePropertyKey, MenuItemRole.SubmenuItem);

                    BreadcrumbItem container = view.ItemContainerGenerator.ContainerFromIndex(index) as BreadcrumbItem;
                    if (container != null /*&& container.IsSelected*/)
                    {
                        menuItem.FontWeight = FontWeights.Bold;
                    }
                    else
                    {
                        menuItem.FontWeight = FontWeights.Normal;
                    }
                }
            }
        }