Exemplo n.º 1
0
        /// <summary>
        /// Returns a context menu containing commands with commandTags</summary>
        /// <param name="commandTags">Command tags for commands to include on menu</param>
        /// <returns>ContextMenu</returns>
        public ContextMenu GetContextMenu(IEnumerable<object> commandTags)
        {
            m_commandService.SuggestRequery();

            var menu = new ContextMenu();
            menu.SetResourceReference(ContextMenu.StyleProperty, Resources.MenuStyleKey);
            //menu.Style = (Style)Application.Current.FindResource(Resources.MenuStyleKey);

            // Generate view model
            List<ICommandItem> commands = new List<ICommandItem>();
            foreach (var tag in commandTags)
            {
                var command = m_commandService.GetCommand(tag);
                if (command != null)
                {
                    if (!AutoCompact || ((ICommand)command).CanExecute(command))
                        commands.Add(command);
                }
            }
            commands.Sort(new CommandComparer());

            var dummyRootMenu = new Sce.Atf.Wpf.Models.Menu(null, null, null, null, null);
            foreach (var command in commands)
                MenuUtil.BuildSubMenus(command, dummyRootMenu);

            MenuUtil.InsertGroupSeparators(dummyRootMenu);
            menu.ItemsSource = dummyRootMenu.ChildCollection;

            return menu;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a context menu containing commands with commandTags</summary>
        /// <param name="commandTags">Command tags for commands to include on menu</param>
        /// <returns>ContextMenu</returns>
        public ContextMenu GetContextMenu(IEnumerable<object> commandTags)
        {
            CommandManager.InvalidateRequerySuggested();

            var menu = new ContextMenu();
            menu.SetResourceReference(ContextMenu.StyleProperty, Resources.ContextMenuStyleKey);

            // Generate view model
            var commands = new List<ICommandItem>();
            foreach (object tag in commandTags)
            {
                var command = m_commandService.GetCommand(tag);
                if (command != null)
                {
                    if (!AutoCompact || command.CanExecute(null))
                        commands.Add(command);
                }
                else
                {
                    // Allow direct display of ICommandItems which have not been registered
                    // with the command service
                    var commandItem = tag as ICommandItem;
                    if (commandItem != null && (!AutoCompact || commandItem.CanExecute(null)))
                        commands.Add(commandItem);
                }
            }
            commands.Sort(new CommandComparer());

            var dummyRootMenu = new RootMenuModel(null, null, null);
            foreach (ICommandItem command in commands)
                dummyRootMenu.AddItem(command);

            menu.ItemsSource = dummyRootMenu.Children;

            return menu;
        }