Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 仅在tabcontrol上显示关联菜单_Wpf_Tabcontrol - Fatal编程技术网

Wpf 仅在tabcontrol上显示关联菜单

Wpf 仅在tabcontrol上显示关联菜单,wpf,tabcontrol,Wpf,Tabcontrol,我正在尝试添加一个上下文菜单,它有“Close”和“Close all but this”,就像IE8一样。 当我单击选项卡而不是tabitem时,应显示此菜单 我该怎么做?这就是您需要的: 代码: <TabControl Margin="28,25,57,38" Name="tabControl1"> <TabItem Header="tabItem1" Name="tabItem1"> <TabItem.Context

我正在尝试添加一个上下文菜单,它有“Close”和“Close all but this”,就像IE8一样。
当我单击选项卡而不是tabitem时,应显示此菜单

我该怎么做?

这就是您需要的:

代码:

 <TabControl Margin="28,25,57,38" Name="tabControl1">
        <TabItem Header="tabItem1" Name="tabItem1">
            <TabItem.ContextMenu>
                <ContextMenu Name="ct1" >
                    <MenuItem Name="Item1" Header="Close"/>
                    <MenuItem Name="Item2" Header="CloseOtherThankThis" />
                </ContextMenu>
            </TabItem.ContextMenu>
            <Grid>
                <Label Margin="41,75,22,64" Name="label1">First Tab</Label>
            </Grid>
        </TabItem>
        <TabItem Header="tabItem2" Name="tabItem2">
            <TabItem.ContextMenu>
                <ContextMenu Name="ct2">
                        <MenuItem Name="Item3" Header="Close"/>
                        <MenuItem Name="Item4" Header="CloseOtherThankThis" />
                </ContextMenu>
            </TabItem.ContextMenu>
                 </TabItem>
    </TabControl>

第一选项卡

您是说不应该有重复的上下文菜单吗?

我相信您希望上下文菜单只在用户单击TabItem的标题而不是TabControl的内容区域时出现

如果是这样,您可以为标题定义模板。请参见下面的示例代码

注:
-上下文菜单只在单击TabItHead的文本部分(而不是空白区域的其余部分)时才出现。如果您需要整个选项卡标题区域,则需要修改TabItem的ControlTemplate

示例代码:

<Window x:Class="WpfApplication4.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <DataTemplate x:Key="tabHeaderTemplate">
            <ContentPresenter Width="Auto" Content="{TemplateBinding Content}">
                <ContentPresenter.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Close Tab" />
                        <MenuItem Header="Close Other Tabs" />
                        <Separator />
                        <MenuItem Header="New Tab" />
                    </ContextMenu>
                </ContentPresenter.ContextMenu>
            </ContentPresenter>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <TabControl>
            <TabItem Header="Tab 1"
                     HeaderTemplate="{StaticResource tabHeaderTemplate}">
                    <Label>Data for first Tab goes here</Label>
            </TabItem>
            <TabItem Header="Tab 2"
                     HeaderTemplate="{StaticResource tabHeaderTemplate}">
                <Label>Data for second Tab goes here</Label>
            </TabItem>
            <TabItem Header="Tab 3">
                <Label>Data for third Tab goes here</Label>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

第一个选项卡的数据在这里
第二个选项卡的数据在这里
第三个选项卡的数据在这里

“当我点击选项卡而不是tabitem”这是什么意思?这会导致stackoverflowexception@user156144,stackoveflowexception是否因此代码而发生?奇怪。无论如何,这只是一个例子,您可能想用TextBlock或其他东西替换contentpresenter,或者可能想使用整个ControlTemplate for TabItem方法。我的坏。。。Content={TemplateBinding Content}>给了我一个生成错误,所以我替换了整个Content属性。用Control.Content或其他东西(我不记得了)重新插入它解决了这个问题。