ContextMenu custom item weird display

2 posts, 0 answers
  1. Amand
    Amand avatar
    23 posts
    Member since:
    Aug 2012

    Posted 13 Dec 2016 Link to this post

    Hello,

    I'm trying to add custom menu item to the header cells context menu but i have weird display when i am using derived class instead of RadMenuItem class directly (see attached image).

    Can you explain to me what is wrong with my code ?

    public class CustomOpMenuItem : RadMenuItem
    {
        private MyForm _form;
        private int _columnIndex;
        private CustomOp _op;
     
        public CustomOpMenuItem (MyForm form, int columnIndex, CustomOp op)
        {
            _op = op;
            switch(_op)
            {
                case CustomOp.CUSTOM1:
                    this.Text = "Custom 1";
                    break;
                case CustomOp.CUSTOM2:
                    this.Text = "Custom 2";
                    break;
                case CustomOp.CUSTOM3:
                    this.Text = "Custom 3";
                    break;
            }
     
            _columnIndex = columnIndex;
            _form = form;
            this.Click += onClick;
        }
     
        private void onClick(object sender, EventArgs e)
        {
            _form.DoCustomOp(_columnIndex,_op);
        }
    }
     
    private void onContextMenuOpening(object sender, VirtualGridContextMenuOpeningEventArgs e)
    {  
        for (int i = 0; i < e.ContextMenu.Items.Count; i++)
        {
            if (e.ContextMenu.Items[i].Text == "Pinned state")
            {
                // hide the Delete option from the context menu
                e.ContextMenu.Items[i].Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
                if(i != 0)
                {
                    // hide the separator before the Delete option
                    e.ContextMenu.Items[i - 1].Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
                }
            }
        }
     
        if (e.RowIndex == RadVirtualGrid.HeaderRowIndex)
        {
            RadMenuSeparatorItem separator = new RadMenuSeparatorItem();
            e.ContextMenu.Items.Add(separator);
            RadMenuItem customMenuItem = new RadMenuItem();
            customMenuItem.Text = "Custom Data Operation";
            e.ContextMenu.Items.Add(customMenuItem);
            e.ContextMenu.Items.Add(separator);
            e.ContextMenu.Items.Add(new CustomOpMenuItem(this, e.ColumnIndex, CustomOp.CUSTOM2));
            e.ContextMenu.Items.Add(separator);
            e.ContextMenu.Items.Add(new CustomOpMenuItem(this, e.ColumnIndex, CustomOp.CUSTOM3));
        }
    }

     

    Regards.

  2. Dess
    Admin
    Dess avatar
    2101 posts

    Posted 14 Dec 2016 Link to this post

    Hello Amand,

    Thank you for writing.  

    When you inherit from a RadControl (or any RadControl descendant), the original control themes are not automatically inherited. The good thing is that this behavior can be overridden very easily by overriding the ThemeClassName property of the descendant of RadControl.  When you inherit from a RadElement descendant (for example RadButtonElement), you need to override the ThemeEffectiveType property. This will allow you to have the styling applied to the instance of your custom element class:
    public class CustomOpMenuItem : RadMenuItem
    {
        protected override Type ThemeEffectiveType
        {
            get   
            {
                return typeof(RadMenuItem);    
            }
        }
    //...other code
    }

    The following help article is quite useful on this topic: http://docs.telerik.com/devtools/winforms/telerik-presentation-framework/inherit-themes-from-radcontrols-derivatives

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top