RadDropDownMenu object in RadMenuItem

2 Answers 96 Views
CommandBar
Fabrice
Top achievements
Rank 2
Iron
Iron
Fabrice asked on 24 Oct 2022, 09:17 AM
Hi,

In a RadMenuItem, it seems that the RadDropDownMenu graphical object containing children is automatically created when the MyMenuItem.Items property is accessed.
Often, the Items property is accessed without adding any children, for example to access MyMenuItem.Items.Count property.
RadDropDownMenu object is therefore useless and affects the size of a RadMenuItem in memory.
I'm considering overriding the Items property or the EnsureDropDownCreated method of the RadMenuItemBase class to work around this issue.
Is there a method that you recommend or do not recommend?

We are using 2022.3.921 version.

Thanks for your feedback.

2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 25 Oct 2022, 12:41 PM

Hello, Fabrice,    

The provided feedback is greatly appreciated. 

Actually, by default the HasChildren property returns true or false indicating whether the RadMenuItem contains sub menu items without creating the drop down menu. 

        /// <summary>
        ///		Gets a value indicating whether this item has child items.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool HasChildren
        {
            get
            {
                return (this.dropDown != null && this.dropDown.Items.Count > 0);
            }
        }

So, feel free to use it instead of accessing the RadMenuItem.Items.Count property. If the HasChildren property returns true, then you are free to access the Items collection since the drop down is already initialized.

I believe that it would cover your scenario with the memory consumption. Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Oct 2022, 11:55 AM

Hello, Fabrice,

I would like to share some parts of the internal source code of RadMenuItemBase class:

        /// <summary>
        ///		Gets a collection of the child items.
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public RadItemOwnerCollection Items
        {
            get
            {
                this.EnsureDropDownCreated();
                return this.dropDown.Items;
            }
        }
        protected virtual void EnsureDropDownCreated()
        {
            if (dropDown != null)
                return;

            this.dropDown = this.CreateDropDownMenu();
            this.dropDown.RightToLeft = (this.RightToLeft) ? System.Windows.Forms.RightToLeft.Yes : System.Windows.Forms.RightToLeft.No;
            this.WireDropDownEvents();
            this.OnDropDownCreated();
        }

Indeed, when the Items collection is accessed, it is ensured that the RadDropDownMenu is created even though no items may be added. This is by design and I can't notice any side effect of this behavior. Please correct me if I am wrong. To be honest, usually the drop down menu is expected to be  created together with the RadMenuItem itself.

Please have in mind that the child menu items are stored actually in the drop down. That is why in order to access the Items collection, you need to have the RadDropDownMenu created.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Fabrice
Top achievements
Rank 2
Iron
Iron
commented on 24 Oct 2022, 03:09 PM

Hello,

I also came to this conclusion, but we often use the Items property to get all the submenus recursively.
In this case, creation of DropDownMenu consumes memory even if it's empty.

I worked around the problem by creating a new Items list in the RadMenuItem.
I create the DropDownMenu "on-demand" only when it's not empty.


private bool DropDownInitialized = false;

public new bool HasChildren => DropDownInitialized ? DropDown.Items.Count > 0 : Items_.Count > 0;

protected RadItemOwnerCollection Items_ = new RadItemOwnerCollection();

public new RadItemOwnerCollection Items
{
     get
     {
          EnsureDropDownCreated(false);
          return DropDownInitialized ? DropDown.Items : Items_;
     }
     set => Items_ = value;
}
        

protected override void EnsureDropDownCreated()
{
     EnsureDropDownCreated(true);
}

protected void EnsureDropDownCreated(bool pForce)
{
     if ((!DropDownInitialized && HasChildren) || pForce)
     {
         base.EnsureDropDownCreated();
         if (!DropDownInitialized)
         {
             DropDownInitialized = true;
             DropDown.Items.AddRange(Items_);
             Items_.Clear();
             Items_ = null;
         }
    }
}

Tags
CommandBar
Asked by
Fabrice
Top achievements
Rank 2
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or