Andre Gross
Andre Gross

Reputation: 263

Wpf - Contextmenu in TabItem Header

I wanted to add a contextmenu to a tabitem. But it should only be opened by clicking on the header of the tab. I added the Contextmenu like this:

ContextMenu _contextMenu;
TabItem _tabItem;
//Initialize the components
_tabItem.ContextMenu = _contextMenu;

Only if you klick on the header of the TabItem, the ContextMenu should be opened.

It should only work by right-klicking in the header

But if you klick at another position of the tabItem, it shouldn´t be displayed.

If you don´t klick on the header, it shouldn´t displayed

I need to do that programmatically during the runtime. A solution in xaml is ok, too.

Upvotes: 5

Views: 3655

Answers (1)

Matt Hamilton
Matt Hamilton

Reputation: 204129

How about something like:

_tabItem.Header = new ContentControl
                 {
                     Content = "StartPage",
                     ContextMenu = _contextMenu
                 };

Upvotes: 9

Related Questions