/// <summary> /// Called when a <see cref="T:System.Windows.Controls.Button" /> is clicked. /// </summary> protected override void OnClick() { var actions = GetViewActions(); if (actions == null) { base.OnClick(); return; } var menu = new ContextMenu(); var isFirst = true; foreach (var action in actions.Where(a => a.Availability == ViewActionAvailabilities.Available && a.Visibility == Visibility.Visible)) { var menuItem = new ViewActionMenuItem { DataContext = action, Command = action }; if (action.ActionView == null) { menuItem.Header = GetMenuTitle(action); } else { ElementHelper.DetachElementFromParent(action.ActionView); menuItem.Header = action.ActionView; if (action.ActionViewModel != null) { action.ActionView.DataContext = action.ActionViewModel; } } if (!isFirst && action.BeginGroup) { menu.Items.Add(new Separator()); } if (action.ViewActionType == ViewActionTypes.Toggle) { menuItem.IsCheckable = true; menuItem.IsChecked = action.IsChecked; //menuItem.SetBinding(MenuItem.IsCheckedProperty, new Binding("IsChecked") {Source = action}); } var realAction = action as ViewAction; if (realAction != null && realAction.HasBrush) { var icon = new ThemeIcon { FallbackIconResourceKey = string.Empty }; icon.SetBinding(ThemeIcon.IconResourceKeyProperty, new Binding("BrushResourceKey")); menuItem.Icon = icon; } HandleMenuShortcutKey(menuItem, action); menu.Items.Add(menuItem); isFirst = false; } menu.Placement = PlacementMode.Bottom; menu.PlacementTarget = this; menu.IsOpen = true; }
/// <summary> /// Called when [click]. /// </summary> protected override void OnClick() { base.OnClick(); if (Command != null || SubActions == null) { return; } var menu = new ContextMenu(); var isFirst = true; var replacementBrushes = new ObservableResourceDictionary { { "CODE.Framework-Universe-IconForegroundBrush", FindResource("CODE.Framework-Universe-IconForegroundBrush") } }; foreach (var action in SubActions.Where(a => a.Availability == ViewActionAvailabilities.Available && a.Visibility == Visibility.Visible)) { var menuItem = new ViewActionMenuItem { DataContext = action, Command = action, Header = GetMenuTitle(action) }; if (!isFirst && action.BeginGroup) { menu.Items.Add(new Separator()); } if (action.ViewActionType == ViewActionTypes.Toggle) { menuItem.IsCheckable = true; menuItem.SetBinding(MenuItem.IsCheckedProperty, new Binding("IsChecked") { Source = action }); } var icon = new ThemeIcon { ReplacementBrushes = replacementBrushes }; icon.SetBinding(ThemeIcon.IconResourceKeyProperty, new Binding("BrushResourceKey")); menuItem.Icon = icon; HandleMenuShortcutKey(menuItem, action); menu.Items.Add(menuItem); isFirst = false; } if (IsExpanded) { menu.Placement = PlacementMode.Right; } else { menu.Placement = PlacementMode.Relative; menu.HorizontalOffset = 48d; } menu.PlacementTarget = this; menu.IsOpen = true; }
/// <summary> /// Called when a <see cref="T:System.Windows.Controls.Button" /> is clicked. /// </summary> protected override void OnClick() { var actions = GetViewActions(); if (actions == null) { base.OnClick(); return; } var menu = new ContextMenu(); var isFirst = true; foreach (var action in actions.Where(a => a.Availability == ViewActionAvailabilities.Available && a.Visibility == Visibility.Visible)) { var menuItem = new ViewActionMenuItem {DataContext = action, Command = action, Header = GetMenuTitle(action)}; if (!isFirst && action.BeginGroup) menu.Items.Add(new Separator()); if (action.ViewActionType == ViewActionTypes.Toggle) { menuItem.IsCheckable = true; menuItem.SetBinding(MenuItem.IsCheckedProperty, new Binding("IsChecked") { Source = action }); } var realAction = action as ViewAction; if (realAction != null && realAction.HasBrush) menuItem.Icon = new ViewActionMenuIcon(realAction.PopulatedBrush); HandleMenuShortcutKey(menuItem, action); menu.Items.Add(menuItem); isFirst = false; } menu.Placement = PlacementMode.Bottom; menu.PlacementTarget = this; menu.IsOpen = true; }
/// <summary> /// Adds sub-items for the specified menu item and category /// </summary> /// <param name="menuItem">Parent menu item</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> private void PopulateSubCategories(MenuItem menuItem, ViewActionCategory category, IEnumerable <IViewAction> actions, int indentLevel = 0) { var populatedCategories = new List <string>(); if (actions == null) { return; } var viewActions = actions as IViewAction[] ?? actions.ToArray(); var matchingActions = ViewActionPolicy != null?ViewActionPolicy.GetAllActionsForCategory(viewActions, category, indentLevel, orderByGroupTitle : false, viewModel : this) : ViewActionHelper.GetAllActionsForCategory(viewActions, category, indentLevel, orderByGroupTitle: false); var addedMenuItems = 0; foreach (var matchingAction in matchingActions) { if (addedMenuItems > 0 && matchingAction.BeginGroup) { menuItem.Items.Add(new Separator()); } 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 newMenuItem = new ViewActionMenuItem { Header = matchingAction.Categories[indentLevel + 1].Caption }; var icon = new ThemeIcon { UseFallbackIcon = false }; icon.SetBinding(ThemeIcon.IconResourceKeyProperty, new Binding("BrushResourceKey")); newMenuItem.Icon = icon; CreateMenuItemBinding(matchingAction, newMenuItem); PopulateSubCategories(newMenuItem, matchingAction.Categories[indentLevel + 1], viewActions, indentLevel + 1); menuItem.Items.Add(newMenuItem); addedMenuItems++; } else { var newMenuItem1 = new ViewActionMenuItem { Header = GetMenuTitle(matchingAction), Command = matchingAction, DataContext = matchingAction }; HandleMenuShortcutKey(newMenuItem1, matchingAction); if (matchingAction.ViewActionType == ViewActionTypes.Toggle) { newMenuItem1.IsCheckable = true; newMenuItem1.SetBinding(MenuItem.IsCheckedProperty, new Binding("IsChecked") { Source = matchingAction, Mode = BindingMode.OneWay }); } var realMatchingAction = matchingAction as ViewAction; if (realMatchingAction != null && !string.IsNullOrEmpty(realMatchingAction.ToolTipText)) { newMenuItem1.ToolTip = realMatchingAction.ToolTipText; } var icon = new ThemeIcon { FallbackIconResourceKey = string.Empty }; icon.SetBinding(ThemeIcon.IconResourceKeyProperty, new Binding("BrushResourceKey")); newMenuItem1.Icon = icon; CreateMenuItemBinding(matchingAction, newMenuItem1); menuItem.Items.Add(newMenuItem1); addedMenuItems++; } } }
/// <summary> /// Adds sub-items for the specified menu item and category /// </summary> /// <param name="menuItem">Parent menu item</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> private void PopulateSubCategories(MenuItem menuItem, ViewActionCategory category, IEnumerable<IViewAction> actions, int indentLevel = 0) { var populatedCategories = new List<string>(); if (actions == null) return; var viewActions = actions as IViewAction[] ?? actions.ToArray(); var matchingActions = ViewActionHelper.GetAllActionsForCategory(viewActions, category, indentLevel); var addedMenuItems = 0; foreach (var matchingAction in matchingActions) { if (addedMenuItems > 0 && matchingAction.BeginGroup) menuItem.Items.Add(new Separator()); 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 newMenuItem = new ViewActionMenuItem {Header = matchingAction.Categories[indentLevel+1].Caption}; var realAction = matchingAction as ViewAction; if (realAction != null && realAction.HasBrush) newMenuItem.Icon = new ViewActionMenuIcon(realAction.PopulatedBrush); CreateMenuItemBinding(matchingAction, newMenuItem); PopulateSubCategories(newMenuItem, matchingAction.Categories[indentLevel+1], viewActions, indentLevel + 1); menuItem.Items.Add(newMenuItem); addedMenuItems++; } else { var newMenuItem1 = new ViewActionMenuItem {Header = GetMenuTitle(matchingAction), Command = matchingAction}; HandleMenuShortcutKey(newMenuItem1, matchingAction); if (matchingAction.ViewActionType == ViewActionTypes.Toggle) { newMenuItem1.IsCheckable = true; newMenuItem1.SetBinding(MenuItem.IsCheckedProperty, new Binding("IsChecked") {Source = matchingAction}); } var realAction = matchingAction as ViewAction; if (realAction != null && realAction.HasBrush) newMenuItem1.Icon = new ViewActionMenuIcon(realAction.PopulatedBrush); CreateMenuItemBinding(matchingAction, newMenuItem1); menuItem.Items.Add(newMenuItem1); addedMenuItems++; } } }