Add Context Menu to Tab Control in WPF

You can add a context menu to tab control or to individual tabs. If you add it to tab control it works on anywhere in the tab area. If you add it to a tab item, it only works on that tab item. Take a look at this code:

<TabControl
Name=tcMain >

    <TabControl.ContextMenu>

        <ContextMenu
Name=mnuTabContext1>

            <MenuItem
Header=_Vertical Tab
Name=mnuVerticalTab1></MenuItem>

            <MenuItem
Header=_Horizontal Tab
Name=mnuHorizontalTab1></MenuItem>

        </ContextMenu>

    </TabControl.ContextMenu>

    <TabItem
Name=tabNew
Header=[new]
IsTabStop=False >

        <TabItem.ContextMenu>

            <ContextMenu
Name=mnuTabContext>

                <MenuItem
Header=_Vertical Tab
Name=mnuVerticalTab></MenuItem>

            </ContextMenu>

        </TabItem.ContextMenu>

    </TabItem>

</TabControl>

If you click on the tab control, you get:

But if you click on the tab itself, you get:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s