如何将菜单控件拉伸到WPF中的TabHeader?

问题描述 投票:-1回答:1

以下XAML代码可满足您的测试需求。

<Window x:Class="MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Name="MainWindow" Title="MainWindow" 
Height="200" Width="300">

<Grid>
    <TabControl>
        <TabItem Height="40" Width="50" Margin="0" Padding="0">
            <TabItem.Header>
                <Menu Height="40" Width="50" Margin="0" Padding="0">
                    <MenuItem Height="40" Width="50" Header="File" Margin="0" Padding="0" Background="Red">
                        <MenuItem Header="About"/>
                        <MenuItem Header="Help"/>
                        <MenuItem Header="Print"/>
                    </MenuItem>
                </Menu>
            </TabItem.Header>
            <Label Content="No content here because this Tab will not use."/>
        </TabItem>
        <TabItem Height="40" Width="50" Header="Spain" IsSelected="True">
            <Label Content="Some content for Spain"/>
        </TabItem>
        <TabItem Height="40" Width="50" Header="France">
            <Label Content="Some content for France"/>
        </TabItem>
        <TabItem Height="40" Width="50" Header="Italy">
            <Label Content="Some content for Italy"/>
        </TabItem>
    </TabControl>
</Grid>
</Window>

问题图片:https://prnt.sc/imvkfk

我尝试了以下链接,但它不起作用。

http://www.techerator.com/2011/05/how-to-stretch-a-menu-control-to-the-width-of-a-window-in-wpf/

附:我想要精确伸展。我希望用户无论如何都无法用鼠标点击TabHeader。

c# wpf vb.net xaml
1个回答
0
投票

我觉得我对你想要实现的目标感到有些困惑,但你是否正在尝试这样的事情?用这个替换你的第一个TabItem的代码。如果这是你想要的,请告诉我。如果它不仅仅是进一步解释,我们可以进行调整。

<TabItem Height="40" Width="50" Margin="-3" Padding="0">
            <TabItem.Header>
                <Grid  Height="100" Width="100">
                    <Menu Margin="0" Padding="0">
                        <MenuItem Header="File" Margin="0" Padding="0" Background="Red">
                            <MenuItem Header="About"/>
                            <MenuItem Header="Help"/>
                            <MenuItem Header="Print"/>
                        </MenuItem>
                    </Menu>
                </Grid>

            </TabItem.Header>
            <Label Content="No content here because this Tab will not use."/>
</TabItem>

我只是简单地将菜单包裹在一个网格中,但需要更多控制权。

© www.soinside.com 2019 - 2024. All rights reserved.