Exemplo n.º 1
0
        private int InsertItemAt(DependencyObject item, int priority, ItemCollection items, int n)
        {
            items.Insert(n, item);

            // add separators where necessary, but not on a main menu
            if (ShouldAddSeperators())
            {
                // if before us is a non separator and it's priority is different to ours, then insert a separator
                if (n > 0 && !(items[n - 1] is Separator))
                {
                    int prioBefore = MergeMenus.GetPriority(items[n - 1] as DependencyObject);
                    if (priority != prioBefore)
                    {
                        var separator = new Separator();
                        MergeMenus.SetPriority(separator, priority);
                        items.Insert(n, separator);
                        _AutoCreatedSeparators.Add(separator);
                        ++n;
                    }
                }

                // if after us is a non seperator then add a separator after us
                if (n < items.Count - 1 && !(items[n + 1] is Separator))
                {
                    int prioAfter = MergeMenus.GetPriority(items[n + 1] as DependencyObject);
                    var separator = new Separator();
                    MergeMenus.SetPriority(separator, prioAfter);
                    items.Insert(n + 1, separator);
                    _AutoCreatedSeparators.Add(separator);
                }
            }
            return(n);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if seperators should be added for this host
        /// </summary>
        /// <returns></returns>
        private bool ShouldAddSeperators()
        {
            switch (MergeMenus.GetAddSeparator(HostElement))
            {
            case AddSeparatorBehaviour.Add:
                return(true);

            case AddSeparatorBehaviour.DontAdd:
                return(false);

            default:
                // default is add, except for ToolBarTrays and MainMenus
                return((!(HostElement is ToolBarTray)) && (!(HostElement is Menu) || !(HostElement as Menu).IsMainMenu));
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initialized event handler for hosts
 /// </summary>
 /// <param name="sender">Host</param>
 /// <param name="e">Event args</param>
 /// <remarks>
 /// Adds any known merge items to the host if not already happend.
 /// </remarks>
 private void HostElement_Initialized(object sender, EventArgs e)
 {
     if (HostElement != null)
     {
         var id = MergeMenus.GetId(sender as DependencyObject);
         foreach (var item in MergeMenus.UnmergedItems.ToList())
         {
             if (String.CompareOrdinal(id, MergeMenus.GetHostId(item)) == 0)
             {
                 if (MergeItem(item))
                 {
                     MergeMenus.UnmergedItems.Remove(item);
                 }
             }
         }
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Merges the item into this host
        /// </summary>
        /// <param name="item">Item</param>
        internal bool MergeItem(DependencyObject item)
        {
            bool itemAdded = false;

            // get the priority of the item (if non is attached use highest priority)
            int priority = MergeMenus.GetPriorityDef(item, Int32.MaxValue);

            if (HostElement != null)
            {
                if (HostElement is ToolBarTray)
                {
                    /// special traetment for ToolBarTray hosts becuse a ToolBarTray is no ItemsControl.
                    if (item is ToolBar && !(HostElement as ToolBarTray).ToolBars.Contains(item))
                    {
                        (HostElement as ToolBarTray).ToolBars.Add(item as ToolBar);
                    }
                    _MergedItems.Add(item);
                    itemAdded = true;
                }
                else
                {
                    var items = (HostElement as ItemsControl).Items;
                    // if item is not already in host add it by priority
                    if (!items.Contains(item))
                    {
                        // iterate from behind...
                        for (int n = items.Count - 1; n >= 0; --n)
                        {
                            var d = items[n] as DependencyObject;
                            if (d != null)
                            {
                                // ... and add it after 1st existing item with lower or equal priority
                                if (MergeMenus.GetPriority(d) <= priority)
                                {
                                    itemAdded = true;
                                    n         = InsertItemAt(item, priority, items, n + 1);
                                    break;
                                }
                            }
                        }

                        if (!itemAdded)
                        {
                            InsertItemAt(item, priority, items, 0);
                            itemAdded = true;
                        }
                        _MergedItems.Add(item);

                        // register a VisibilityChanged notifier to hide seperators if necessary
                        if (item is UIElement)
                        {
                            DependencyPropertyDescriptor.FromProperty(UIElement.VisibilityProperty, item.GetType()).AddValueChanged(item, Item_VisibilityChanged);
                        }
                        CheckSeparatorVisibility(true);
                    }
                    else
                    {
                        itemAdded = true;
                    }
                }
            }
            return(itemAdded);
        }