private static void BuildExtensibleMenuItem(ICommandBar parentMenu, Microsoft.Windows.Design.Interaction.MenuBase menuItem, ref bool lastWasSeperator, ref bool anythingAdded)
        {
            MenuAction menuAction = menuItem as MenuAction;
            MenuGroup  menuGroup  = menuItem as MenuGroup;

            if (menuAction != null)
            {
                if (!menuAction.Visible)
                {
                    return;
                }
                ICommandBarButton commandBarButton = parentMenu.Items.AddButton("ExtensibilityCommand");
                commandBarButton.Text = menuAction.DisplayName;
                MenuItem menuItem1 = (MenuItem)commandBarButton;
                menuItem1.Command     = menuAction.Command;
                menuItem1.IsEnabled   = menuAction.Enabled;
                menuItem1.IsCheckable = menuAction.Checkable;
                menuItem1.IsChecked   = menuAction.Checked;
                lastWasSeperator      = false;
                anythingAdded         = true;
            }
            else
            {
                if (menuGroup == null)
                {
                    return;
                }
                ICommandBar parentMenu1;
                if (menuGroup.HasDropDown)
                {
                    parentMenu1      = (ICommandBar)parentMenu.Items.AddMenu("ExtensibilityMenu", menuGroup.DisplayName);
                    anythingAdded    = true;
                    lastWasSeperator = false;
                }
                else
                {
                    parentMenu1 = parentMenu;
                    if (anythingAdded && !lastWasSeperator)
                    {
                        parentMenu.Items.AddSeparator();
                        lastWasSeperator = true;
                    }
                }
                foreach (Microsoft.Windows.Design.Interaction.MenuBase menuItem1 in (Collection <Microsoft.Windows.Design.Interaction.MenuBase>)menuGroup.Items)
                {
                    ContextMenuHelper.BuildExtensibleMenuItem(parentMenu1, menuItem1, ref lastWasSeperator, ref anythingAdded);
                }
                if (menuGroup.HasDropDown)
                {
                    return;
                }
                if (!lastWasSeperator && anythingAdded)
                {
                    parentMenu.Items.AddSeparator();
                    lastWasSeperator = true;
                }
                else
                {
                    lastWasSeperator = false;
                }
            }
        }
        private static void InvokeSelectionContextMenu(UIElement scopeElement, SceneViewModel sceneViewModel, Point contextMenuPosition)
        {
            ContextMenu selectionContextMenu = ContextMenuHelper.CreateSelectionContextMenu(sceneViewModel);

            ContextMenuHelper.InvokeContextMenu(scopeElement, contextMenuPosition, selectionContextMenu);
        }
        public static ContextMenu CreateContextMenu(ISelectionSet <SceneElement> selection, SceneViewModel viewModel, bool isOnArtboard)
        {
            viewModel.DesignerContext.CommandBarService.CommandBars.Remove("Designer_SceneContextMenu");
            ICommandBar commandBar = viewModel.DesignerContext.CommandBarService.CommandBars.AddContextMenu("Designer_SceneContextMenu");

            commandBar.Items.AddButton("Edit_Cut", StringTable.ElementContextMenuCut);
            commandBar.Items.AddButton("Edit_Copy", StringTable.ElementContextMenuCopy);
            commandBar.Items.AddButton("Edit_Paste", StringTable.ElementContextMenuPaste);
            commandBar.Items.AddButton("Edit_Delete", StringTable.ElementContextMenuDelete);
            ContextMenuHelper.AddSeparatorHelper.AddSeparator(commandBar);
            ContextMenuHelper.AddExtensibleMenuItems(commandBar, viewModel, selection.PrimarySelection != null ? selection.PrimarySelection.TargetType : (Type)null);
            if (new CopyToResourceCommand(viewModel).IsEnabled)
            {
                commandBar.Items.AddButton("Edit_MakeTileBrush_CopyToResource", StringTable.ElementContextMenuCopyToResource);
            }
            if (new MoveToResourceCommand(viewModel).IsEnabled)
            {
                commandBar.Items.AddButton("Edit_MakeTileBrush_MoveToResource", StringTable.ElementContextMenuMoveToResource);
            }
            bool flag1 = false;
            bool flag2 = false;
            bool flag3 = true;

            foreach (SceneElement element in selection.Selection)
            {
                if (!flag1 && PathConversionHelper.CanConvert(element))
                {
                    flag1 = true;
                    flag2 = true;
                }
                if (!flag2 && element.IsSet(Base2DElement.ClipProperty) == PropertyState.Set)
                {
                    flag1 = true;
                }
                if (flag3 && !PlatformTypes.UIElement.IsAssignableFrom((ITypeId)element.Type))
                {
                    flag3 = false;
                }
            }
            if (flag3)
            {
                ICommandBarMenu commandBarMenu1 = commandBar.Items.AddMenu("Order", StringTable.ElementContextMenuOrder);
                commandBarMenu1.Items.AddButton("Edit_Order_BringToFront", StringTable.ElementContextMenuOrderBringToFront);
                commandBarMenu1.Items.AddButton("Edit_Order_BringForward", StringTable.ElementContextMenuOrderBringForward);
                commandBarMenu1.Items.AddButton("Edit_Order_SendBackward", StringTable.ElementContextMenuOrderSendBackward);
                commandBarMenu1.Items.AddButton("Edit_Order_SendToBack", StringTable.ElementContextMenuOrderSendToBack);
                ICommandBarMenu commandBarMenu2 = commandBar.Items.AddMenu("Align", StringTable.ElementContextMenuAlign);
                commandBarMenu2.Items.AddButton("Edit_Align_AlignLeft", StringTable.ElementContextMenuAlignLeft);
                commandBarMenu2.Items.AddButton("Edit_Align_AlignCenter", StringTable.ElementContextMenuAlignCenter);
                commandBarMenu2.Items.AddButton("Edit_Align_AlignRight", StringTable.ElementContextMenuAlignRight);
                commandBarMenu2.Items.AddSeparator();
                commandBarMenu2.Items.AddButton("Edit_Align_AlignTop", StringTable.ElementContextMenuAlignTop);
                commandBarMenu2.Items.AddButton("Edit_Align_AlignMiddle", StringTable.ElementContextMenuAlignMiddle);
                commandBarMenu2.Items.AddButton("Edit_Align_AlignBottom", StringTable.ElementContextMenuAlignBottom);
                ICommandBarMenu commandBarMenu3 = commandBar.Items.AddMenu("AutoSize", StringTable.ElementContextMenuAutoSize);
                commandBarMenu3.Items.AddCheckBox("Edit_AutoSize_Horizontal", StringTable.ElementContextMenuAutoSizeWidth);
                commandBarMenu3.Items.AddCheckBox("Edit_AutoSize_Vertical", StringTable.ElementContextMenuAutoSizeHeight);
                commandBarMenu3.Items.AddCheckBox("Edit_AutoSize_Both", StringTable.ElementContextMenuAutoSizeBoth);
                commandBarMenu3.Items.AddButton("Edit_AutoSize_Fill", StringTable.ElementContextMenuAutoSizeFill);
            }
            if (flag3)
            {
                ContextMenuHelper.AddSeparatorHelper.AddSeparator(commandBar);
                commandBar.Items.AddButton("Edit_Group", StringTable.ElementContextMenuGroup);
                commandBar.Items.AddDynamicMenu("Edit_GroupInto", StringTable.GroupIntoCommandName);
                commandBar.Items.AddButton("Edit_Ungroup", StringTable.ElementContextMenuUngroup);
            }
            if (ChangeLayoutTypeFlyoutCommand.ShouldShowChangeLayoutTypeMenu(selection))
            {
                commandBar.Items.AddDynamicMenu("Edit_ChangeLayoutTypes", StringTable.ChangeLayoutTypeCommandName);
            }
            if (new ToggleLockInsertionPointCommand(viewModel).IsEnabled)
            {
                commandBar.Items.AddCheckBox("Edit_LockInsertionPoint", StringTable.SelectionContextMenuLockInsertionPoint);
            }
            if (isOnArtboard)
            {
                commandBar.Items.AddDynamicMenu("Edit_SetCurrentSelection", StringTable.ElementContextMenuSetCurrentSelection);
            }
            ContextMenuHelper.AddSeparatorHelper.AddSeparator(commandBar);
            if (flag2)
            {
                ICommandBarMenu menu = commandBar.Items.AddMenu("Combine", StringTable.ElementContextMenuToolsMenuCombine);
                menu.Items.AddButton("Tools_Combine_Unite", StringTable.ElementContextMenuToolsMenuUnite);
                menu.Items.AddButton("Tools_Combine_Divide", StringTable.ElementContextMenuToolsMenuDivide);
                menu.Items.AddButton("Tools_Combine_Intersect", StringTable.ElementContextMenuToolsMenuIntersect);
                menu.Items.AddButton("Tools_Combine_Subtract", StringTable.ElementContextMenuToolsMenuSubtract);
                menu.Items.AddButton("Tools_Combine_ExcludeOverlap", StringTable.ElementContextMenuToolsMenuExcludeOverlap);
                if (ContextMenuHelper.IsSubmenuDisabled(menu))
                {
                    commandBar.Items.Remove((ICommandBarItem)menu);
                }
            }
            if (flag1)
            {
                ICommandBarMenu menu = commandBar.Items.AddMenu("Path", StringTable.ElementContextMenuObjectMenuPath);
                menu.Items.AddButton("Edit_ConvertToPath", StringTable.ElementContextMenuObjectMenuConvertToPath);
                menu.Items.AddButton("Edit_ConvertToMotionPath", StringTable.ElementContextMenuObjectMenuConvertToMotionPath);
                menu.Items.AddButton("Edit_MakeLayoutPath", StringTable.ElementContextMenuObjectMenuMakeLayoutPath);
                menu.Items.AddButton("Edit_MakeClippingPath", StringTable.ElementContextMenuObjectMenuMakeClippingPath);
                menu.Items.AddButton("Edit_RemoveClippingPath", StringTable.ElementContextMenuObjectMenuRemoveClippingPath);
                menu.Items.AddButton("Edit_MakeCompoundPath", StringTable.ElementContextMenuObjectMenuMakeCompoundPath);
                menu.Items.AddButton("Edit_ReleaseCompoundPath", StringTable.ElementContextMenuObjectMenuReleaseCompoundPath);
                if (ContextMenuHelper.IsSubmenuDisabled(menu))
                {
                    commandBar.Items.Remove((ICommandBarItem)menu);
                }
            }
            ContextMenuHelper.AddSeparatorHelper.AddSeparator(commandBar);
            if (selection.Count == 1)
            {
                SceneElement primarySelection = selection.PrimarySelection;
                int          count            = commandBar.Items.Count;
                primarySelection.AddCustomContextMenuCommands(commandBar);
                if (count == commandBar.Items.Count)
                {
                    commandBar.Items.RemoveAt(count - 1);
                }
                if (primarySelection.DocumentNode != null)
                {
                    if (TextEditProxyFactory.IsEditableElement(primarySelection))
                    {
                        commandBar.Items.AddButton("Edit_EditText", StringTable.ElementContextMenuEditText);
                    }
                    if (flag3)
                    {
                        ContextMenuHelper.AddSeparatorHelper.AddSeparator(commandBar);
                        commandBar.Items.AddButton("Edit_MakeButton", StringTable.MakeControlCommandName);
                        if (viewModel.PartsModel.IsEnabled && selection.Count == 1)
                        {
                            if (viewModel.PartsModel.GetPartStatus((SceneNode)selection.PrimarySelection) != PartStatus.Assigned)
                            {
                                Type targetElementType = ((FrameworkTemplateElement)viewModel.ActiveEditingContainer).TargetElementType;
                                commandBar.Items.AddDynamicMenu("Edit_MakePart", string.Format((IFormatProvider)CultureInfo.CurrentCulture, StringTable.MakeIntoPartMenuEnabled, new object[1]
                                {
                                    (object)targetElementType.Name
                                }));
                            }
                            else
                            {
                                commandBar.Items.AddButton("Edit_MakePart", StringTable.MakeIntoPartMenuDisabled);
                            }
                            commandBar.Items.AddButton("Edit_ClearPart", StringTable.ClearPartAssignmentCommandName);
                        }
                        commandBar.Items.AddButton("Edit_MakeUserControl", StringTable.MakeUserControlCommandName);
                        commandBar.Items.AddButton("Edit_MakeCompositionScreen", StringTable.MakeCompositionScreenCommandName);
                        if (primarySelection.DocumentNode.Type.XamlSourcePath != null)
                        {
                            commandBar.Items.AddButton("Edit_EditControl", StringTable.ElementContextMenuEditControl);
                        }
                    }
                }
                ITypeId   type      = (ITypeId)primarySelection.Type;
                StyleNode styleNode = primarySelection as StyleNode;
                if (styleNode != null)
                {
                    type = (ITypeId)styleNode.StyleTargetTypeId;
                }
                if (PlatformTypes.Control.IsAssignableFrom(type) || PlatformTypes.Page.IsAssignableFrom(type))
                {
                    ContextMenuHelper.AddSeparatorHelper.AddSeparator(commandBar);
                    ICommandBarMenu commandBarMenu = commandBar.Items.AddMenu("Template", StringTable.ElementContextMenuEditTemplateMenuName);
                    commandBarMenu.Items.AddButton("Edit_Template_EditExisting", StringTable.EditExistingTemplateCommandName);
                    commandBarMenu.Items.AddButton("Edit_Template_EditCopy", StringTable.EditClonedStyleCommandName);
                    commandBarMenu.Items.AddButton("Edit_Template_EditNew", StringTable.EditEmptyStyleCommandName);
                    commandBarMenu.Items.AddSeparator();
                    commandBarMenu.Items.AddDynamicMenu("Edit_Template_LocalResource", StringTable.ElementContextMenuEditLocalResource);
                    commandBar.Items.AddDynamicMenu("Edit_EditTemplates", StringTable.ElementContextMenuEditTemplates);
                }
                else if (PlatformTypes.FrameworkElement.IsAssignableFrom(type))
                {
                    ContextMenuHelper.AddSeparatorHelper.AddSeparator(commandBar);
                    ICommandBarMenu commandBarMenu = commandBar.Items.AddMenu("Style", StringTable.ElementContextMenuEditStyleMenuName);
                    commandBarMenu.Items.AddButton("Edit_Style_EditExisting", StringTable.ElementContextMenuEditExistingStyle);
                    commandBarMenu.Items.AddButton("Edit_Style_EditCopy", StringTable.ElementContextMenuEditCopyStyle);
                    commandBarMenu.Items.AddButton("Edit_Style_EditNew", StringTable.ElementContextMenuEditNewStyle);
                    commandBarMenu.Items.AddSeparator();
                    commandBarMenu.Items.AddDynamicMenu("Edit_Style_LocalResource", StringTable.ElementContextMenuEditLocalResource);
                }
            }
            else if (flag3)
            {
                ContextMenuHelper.AddSeparatorHelper.AddSeparator(commandBar);
                commandBar.Items.AddButton("Edit_MakeUserControl", StringTable.MakeUserControlCommandName);
                commandBar.Items.AddButton("Edit_MakeCompositionScreen", StringTable.MakeCompositionScreenCommandName);
            }
            if (viewModel.DefaultView.ViewMode == ViewMode.Design)
            {
                ContextMenuHelper.AddSeparatorHelper.AddSeparator(commandBar);
                commandBar.Items.AddButton("View_GoToXaml", StringTable.ViewXamlCommandName);
            }
            IPrototypingService prototypingService = viewModel.DesignerContext.PrototypingService;

            if (prototypingService != null)
            {
                prototypingService.AddElementContextMenuItems(commandBar, (IEnumerable <SceneElement>)selection.Selection, viewModel, isOnArtboard);
            }
            ContextMenu contextMenu = (ContextMenu)commandBar;

            contextMenu.MinWidth = 200.0;
            ContextMenuHelper.AddSeparatorHelper.RemoveLastSeparatorIfNeeded(commandBar);
            return(contextMenu);
        }