/// <summary>
        /// 基于指定控件自动注册Ribbon下的Tab.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <returns></returns>
        public static bool Register(FrameworkElement frameworkElement)
        {
            var mainRibbon = Autodesk.Windows.ComponentManager.Ribbon;

            if (mainRibbon == null)
            {
                return(false);
            }

            var ribbons = new List <KeyValuePair <RibbonControl, object> >();

            if (frameworkElement is RibbonControl ribbon)
            {
                ribbons.Add(new KeyValuePair <RibbonControl, object>(ribbon, ribbon.DataContext));
            }
            else
            {
                var temps = new List <RibbonControl>();

                frameworkElement.GetRibbonControl(ref temps);

                foreach (var item in temps)
                {
                    var dataContext = frameworkElement.DataContext;
                    if (dataContext == null)
                    {
                        dataContext = item.DataContext;
                    }

                    ribbons.Add(new KeyValuePair <RibbonControl, object>(item, dataContext));
                }
            }

            if (ribbons.Count == 0)
            {
                return(false);
            }

            var eum = ribbons.GetEnumerator();

            while (eum.MoveNext())
            {
                var tabs = eum.Current.Key.Tabs;

                RibbonBindingHelper.Binding(eum.Current.Key, eum.Current.Value);

                foreach (var item in tabs)
                {
                    mainRibbon.Tabs.Add(item);
                }
            }
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// 递归遍历Item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="dataContext">The data context.</param>

        private static void ItemRecursive(RibbonItem item, object dataContext)
        {
            if (item == null)
            {
                return;
            }

            switch (item)
            {
            case RibbonListButton listButton:

                listButton.CommandHandler = new NonCommand();

                for (int i = 0; i < listButton.Items.Count; i++)
                {
                    RibbonBindingHelper.ItemRecursive(listButton.Items[i], dataContext);
                }

                break;

            case ToolBarHistoryButton toolBar:

                RibbonBindingHelper.ItemBinding(item, dataContext);

                for (int i = 0; i < toolBar.Items.Count; i++)
                {
                    var current = toolBar.Items[i] as RibbonItem;

                    if (current == null)
                    {
                        continue;
                    }

                    RibbonBindingHelper.ItemRecursive(current, dataContext);
                }

                break;

            case RibbonMenuItem menuItem:

                for (int i = 0; i < menuItem.Items.Count; i++)
                {
                    RibbonBindingHelper.ItemRecursive(menuItem.Items[i], dataContext);
                }

                break;

            case RibbonList list:


                for (int i = 0; i < list.Items.Count; i++)
                {
                    var current = list.Items[i] as RibbonItem;

                    if (current == null)
                    {
                        continue;
                    }
                    RibbonBindingHelper.ItemRecursive(current, dataContext);
                }

                break;

            case RibbonRowPanel rowPanel:

                for (int i = 0; i < rowPanel.Items.Count; i++)
                {
                    RibbonBindingHelper.ItemRecursive(rowPanel.Items[i], dataContext);
                }

                break;

            default:

                RibbonBindingHelper.ItemBinding(item, dataContext);

                break;
            }
        }