仅在TabControl中的Tab中添加右键菜单

若想实现仅在TabControl中的Tab中添加右键菜单,可在XAML中通过使用样式得到:

 

<TabControl>
    <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu/> <!-- Define it here! -->
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

 

但是上面的菜单执行CLICK事件时会无效。把菜单放到控件资源里就好了:

 

<UserControl.Resources>
        <ResourceDictionary>
            
            <ContextMenu x:Key="SheetTabContextMenu">
                <ContextMenu.Items>
                    <MenuItem Name="DeleteSheetMenu" Header="删除" Click="DeleteSheetMenu_OnClick" />
                </ContextMenu.Items>
            </ContextMenu>

</ResourceDictionary>
</UserControl.Resources>

 

<TabControl Name="SheetTabControl">
                        <TabControl.ItemContainerStyle>
                            <Style TargetType="{x:Type TabItem}">
                                <Setter Property="ContextMenu" Value="{StaticResource SheetTabContextMenu}" />
                            </Style>
                        </TabControl.ItemContainerStyle>
                    </TabControl>

 

你可能感兴趣的:(右键菜单)