How to add a Context Menu to a Tab Control in WPF

TabItem.ContextMenu is used to add a ContextMenu to a TabItem. The following code snippet creates a TabControl where the first TabItem has a ContextMenu with three Menu items.

<TabControl>
    <TabItem Name="ColorTabItem" Header="Color Tab">
        <TabItem.ContextMenu>
            <ContextMenu MenuItem.Click="ContextMenuClickEventHandler">
                <MenuItem Header="Red" Name="RedMenuItem"/>
                <MenuItem Header="Blue" Name="BlueMenuItem"/>
                <MenuItem Header="Orange" Name="OrangeMenuItem"/>
            </ContextMenu>
        </TabItem.ContextMenu>
        <TabItem.Content>
            Tab Item data here
        </TabItem.Content>
    </TabItem>
    <TabItem Name="ShapeTabItem" Header="Shape Tab"></TabItem>
</TabControl>


Write this click event handler to your code behind.

void ContextMenuClickEventHandler(object sender, RoutedEventArgs e)
{
    if (e.Source == RedMenuItem )
    {               
        ColorTabItem.Header = "Red Item";
        ColorTabItem.Foreground = Brushes.Red;
    }
    else if (e.Source == BlueMenuItem )
    {
        ColorTabItem.Header = "Blue Item";
        ColorTabItem.Foreground = Brushes.Blue;
    }
    else if (e.Source == OrangeMenuItem )
    {
        ColorTabItem.Header = "Orange Item";
        ColorTabItem.Foreground = Brushes.Orange;
    }
}