仅在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>

 

posted @ 2015-03-06 19:11  马语者  阅读(2254)  评论(0编辑  收藏  举报