/// <summary> /// Adds sub-items for the specified tab item and category /// </summary> /// <param name="parentPanel">Parent item container</param> /// <param name="category">Category we are interested in</param> /// <param name="actions">Actions to consider</param> /// <param name="indentLevel">Current hierarchical indentation level</param> /// <param name="ribbonPage">The ribbon page.</param> /// <param name="visibilityBinding">The visibility binding.</param> protected virtual void PopulateSubCategories(Panel parentPanel, ViewActionCategory category, IEnumerable<IViewAction> actions, int indentLevel = 0, RibbonPage ribbonPage = null, MultiBinding visibilityBinding = null) { var populatedCategories = new List<string>(); if (actions == null) return; var viewActions = actions as IViewAction[] ?? actions.ToArray(); var matchingActions = ViewActionHelper.GetAllActionsForCategory(viewActions, category, indentLevel, EmptyGlobalCategoryTitle); var addedMenuItems = 0; foreach (var matchingAction in matchingActions) { if (addedMenuItems > 0 && matchingAction.BeginGroup) parentPanel.Children.Add(new RibbonSeparator()); if (matchingAction.Categories != null && matchingAction.Categories.Count > indentLevel + 1 && !populatedCategories.Contains(matchingAction.Categories[indentLevel].Id)) // This is further down in a sub-category even { populatedCategories.Add(matchingAction.Categories[indentLevel].Id); var newRibbonButton = new RibbonButtonLarge {Content = matchingAction.Categories[indentLevel].Caption, Visibility = matchingAction.Visibility}; CreateMenuItemBinding(matchingAction, newRibbonButton); if (visibilityBinding != null) visibilityBinding.Bindings.Add(new Binding("Visibility") {Source = newRibbonButton}); PopulateSubCategories(parentPanel, matchingAction.Categories[indentLevel], viewActions, indentLevel + 1); newRibbonButton.AccessKey = matchingAction.AccessKey.ToString(CultureInfo.CurrentUICulture).Trim().ToUpper(); parentPanel.Children.Add(newRibbonButton); addedMenuItems++; } else { var realAction = matchingAction as ViewAction; Brush iconBrush = Brushes.Transparent; if (realAction != null) iconBrush = realAction.Brush; if (matchingAction.ActionView != null) { if (matchingAction.ActionViewModel != null && matchingAction.ActionView.DataContext == null) matchingAction.ActionView.DataContext = matchingAction.ActionViewModel; var command = GetRibbonItemCommand(matchingAction.ActionView); if (command == null) SetRibbonItemCommand(matchingAction.ActionView, matchingAction); if (visibilityBinding != null) visibilityBinding.Bindings.Add(new Binding("Visibility") {Source = matchingAction.ActionView}); ElementHelper.DetachElementFromParent(matchingAction.ActionView); if (matchingAction.ActionView.Style == null) { var resource = TryFindResource("CODE.Framework-Ribbon-CustomControlContainerStyle"); if (resource != null) { var genericStyle = resource as Style; if (genericStyle != null) matchingAction.ActionView.Style = genericStyle; } } matchingAction.ActionView.IsVisibleChanged += (s, e) => InvalidateAll(); parentPanel.Children.Add(matchingAction.ActionView); } else { RibbonButton newRibbonButton; if (matchingAction.Significance == ViewActionSignificance.AboveNormal || matchingAction.Significance == ViewActionSignificance.Highest) newRibbonButton = new RibbonButtonLarge { Content = matchingAction.Caption, Command = matchingAction, Icon = iconBrush, Visibility = matchingAction.Visibility }; else newRibbonButton = new RibbonButtonSmall { Content = matchingAction.Caption, Command = matchingAction, Icon = iconBrush, Visibility = matchingAction.Visibility }; newRibbonButton.IsVisibleChanged += (s, e) => InvalidateAll(); newRibbonButton.SetBinding(ContentControl.ContentProperty, new Binding("Caption") {Source = matchingAction, Mode = BindingMode.OneWay}); CreateMenuItemBinding(matchingAction, newRibbonButton); if (visibilityBinding != null) visibilityBinding.Bindings.Add(new Binding("Visibility") {Source = newRibbonButton}); if (matchingAction.ViewActionType == ViewActionTypes.Toggle) newRibbonButton.SetBinding(RibbonButton.IsCheckedProperty, new Binding("IsChecked") {Source = matchingAction}); if (matchingAction.AccessKey != ' ') newRibbonButton.AccessKey = matchingAction.AccessKey.ToString(CultureInfo.CurrentUICulture).Trim().ToUpper(); parentPanel.Children.Add(newRibbonButton); HandleRibbonShortcutKey(newRibbonButton, matchingAction, ribbonPage); } addedMenuItems++; } } if (addedMenuItems > 0) parentPanel.Children.Add(new RibbonSeparator()); }
/// <summary> /// Handles the assignment of shortcut keys /// </summary> /// <param name="button">The button.</param> /// <param name="action">The category.</param> /// <param name="ribbonPage">The ribbon page.</param> protected virtual void HandleRibbonShortcutKey(RibbonButton button, IViewAction action, RibbonPage ribbonPage) { if (action.ShortcutKey == Key.None) return; MenuKeyBindings.Add(new ViewActionMenuKeyBinding(action)); }
/// <summary> /// Populates the current ribbon with items based on the actions collection /// </summary> /// <param name="actions">List of primary actions</param> /// <param name="actions2">List of view specific actions</param> /// <param name="selectedViewTitle">The selected view title.</param> protected virtual void PopulateRibbon(IHaveActions actions, IHaveActions actions2 = null, string selectedViewTitle = "") { var oldSelectedPage = SelectedIndex; RemoveAllMenuKeyBindings(); Items.Clear(); if (actions == null) return; var actionList = ViewActionHelper.GetConsolidatedActions(actions, actions2, selectedViewTitle); var rootCategories = ViewActionHelper.GetTopLevelActionCategories(actionList, EmptyGlobalCategoryTitle, EmptyLocalCategoryTitle); var pageCounter = 0; var selectedIndex = -1; var standardSelectedIndexSet = false; var specialSelectedIndexSet = false; var viewActionCategories = rootCategories as ViewActionCategory[] ?? rootCategories.ToArray(); foreach (var category in viewActionCategories) { RibbonPage tab; var tabVisibilityBinding = new MultiBinding {Converter = new MaximumVisibilityMultiConverter()}; if (category.IsLocalCategory && HighlightLocalCategories) { var caption = category.Caption.ToUpper(); if (string.IsNullOrEmpty(caption)) caption = ForceTopLevelTitlesUpperCase ? selectedViewTitle.Trim().ToUpper() : selectedViewTitle.Trim(); tab = new RibbonSpecialPage {Header = caption}; if (!specialSelectedIndexSet) { selectedIndex = pageCounter; specialSelectedIndexSet = true; } } else { if (pageCounter == 0 && FirstPageIsSpecial) tab = new RibbonFirstPage {Header = ForceTopLevelTitlesUpperCase ? category.Caption.Trim().ToUpper() : category.Caption.Trim()}; else { tab = new RibbonPage {Header = ForceTopLevelTitlesUpperCase ? category.Caption.Trim().ToUpper() : category.Caption.Trim()}; if (!standardSelectedIndexSet && !specialSelectedIndexSet) { selectedIndex = pageCounter; standardSelectedIndexSet = true; } } } var items = new RibbonPageLayoutPanel(); tab.Content = items; PopulateSubCategories(items, category, actionList, ribbonPage: tab, visibilityBinding: tabVisibilityBinding); Items.Add(tab); tab.SetBinding(VisibilityProperty, tabVisibilityBinding); if (category.AccessKey != ' ') { var pageIndexToSelect = pageCounter; var pageAccessKey = (Key) Enum.Parse(typeof (Key), category.AccessKey.ToString(CultureInfo.InvariantCulture).ToUpper()); MenuKeyBindings.Add(new ViewActionMenuKeyBinding(new ViewAction(execute: (a, o) => { SelectedIndex = pageIndexToSelect; var window = ElementHelper.FindParent<Window>(this) ?? ElementHelper.FindVisualTreeParent<Window>(this); SetKeyboardShortcutsActive(window, true); ReadyForStatusChange = false; }) { ShortcutKey = pageAccessKey, ShortcutModifiers = ModifierKeys.Alt })); tab.PageAccessKey = category.AccessKey.ToString(CultureInfo.InvariantCulture).Trim().ToUpper(); } pageCounter++; } // We are checking for a selected default page pageCounter = 0; if (actionList.Count(a => a.IsDefaultSelection) > 0) foreach (var category in viewActionCategories) { var matchingActions = ViewActionHelper.GetAllActionsForCategory(actionList, category); foreach (var matchingAction in matchingActions) if (matchingAction.IsDefaultSelection) { selectedIndex = pageCounter; break; } pageCounter++; } if (selectedIndex == -1) selectedIndex = oldSelectedPage; if (selectedIndex == -1) selectedIndex = 0; if (selectedIndex >= Items.Count) selectedIndex = Items.Count - 1; LastRegularIndex = selectedIndex; SelectedIndex = selectedIndex; CreateAllMenuKeyBindings(); }