We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Add menu item to tab context menu

Hi, when I right click on tab, I can see default context menu like: Close, Close all, Close all but this etc.
I was wondering, how can I add my custom menu items to the default one?

4 Replies

JP Jagadeesan Pichaimuthu Syncfusion Team April 23, 2019 06:51 AM UTC

Hi Adam, 

Thanks for using Syncfusion products. 

You can able to add the custom context menu items while loading the document container. Please find the below code snippet where the new menu item is added with existing items. 

documentContainer.Loaded += DocumentContainer_Loaded; 
 
private void DocumentContainer_Loaded(object sender, RoutedEventArgs e) 
{ 
    DocumentTabControl tabControl = VisualUtils.FindDescendant(sender as Visual, typeof(DocumentTabControl)) as DocumentTabControl; 
    foreach (var item in tabControl.Items) 
    { 
        if (item is TabItemExt) 
        { 
            TabItemExt tabItem = item as TabItemExt; 
            CustomMenuItem menuItem = new CustomMenuItem(); 
            menuItem.Header = "CustomMenuItem"; 
            if (tabItem != null && tabItem.Header.ToString() == "Integration") 
            { 
                tabItem.ContextMenuItems.Add(menuItem); 
            } 
        } 
    } 
} 



Please find the sample from below location for your reference. 



 


Let us know whether this helps also if you need any further assistance on this. 

Regards, 
Jagadeesan


AD Adam April 23, 2019 11:32 AM UTC

It doesn't work the way I thought.
Menu is created only for statically created tabs in XAML.
But I create also tabs dynamically in code, and those tabs don't get it my custom menu.

This is how I create the tabs:

DocumentControl dc = new DocumentControl(viewModel.Localizer);
dc.DataContext = docViewModel;
docContainer.Items.Add(dc);
DocumentContainer.SetHeader(dc, docViewModel.Document);

And now the question is - how to apply my custom menu to dynamically created tabs?


AD Adam April 23, 2019 12:05 PM UTC

OK, with your code I was able to resolve this:

DocumentControl dc = new DocumentControl(viewModel.Localizer);
dc.DataContext = docViewModel;
docContainer.Items.Add(dc);
DocumentContainer.SetHeader(dc, docViewModel.Document);

AddMenusToDocumentTab(tabControl.Items[tabControl.Items.Count -1] as TabItemExt);

You just have to make sure that you add your tabs at the end of the list.
And AddMenus... is simple as:


CustomMenuItem result = new CustomMenuItem();
result.Header = name;
result.Command = cmdToExecute;

tabItem.ContextMenuItems.Add(result);



JP Jagadeesan Pichaimuthu Syncfusion Team April 23, 2019 12:13 PM UTC

Hi Adam, 
 
Thanks for your update. 
 
We are glad to know that the provided solution worked at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Jagadeesan 


Loader.
Live Chat Icon For mobile
Up arrow icon