在MenuItem内的所有subMenuItem上垂直旋转Label

时间:2016-11-16 14:51:48

标签: wpf xaml menu menuitem

我的应用中有一个菜单,其中包含少量MenuItem。我想在所有MenuItem的左侧看一个垂直标签。像这样(当然有灰色背景,原谅我的绘画技巧): wanted output

我已经这样试过了:

    <Menu IsMainMenu="True" Grid.Row="0" Grid.ColumnSpan="3">
        <MenuItem Header="_File">
            <TextBlock Text="Type1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,0">
                <TextBlock.LayoutTransform>
                    <RotateTransform Angle="-90"/>
                </TextBlock.LayoutTransform>
            </TextBlock>
            <MenuItem Header="_Open" Command="{Binding OpenFileCommand}" IsEnabled="{Binding CanOpenFile}"/>
            <Separator/>
            <MenuItem Header="_Save" Command="{Binding SaveFileCommand}"/>
            <MenuItem Header="_Save As" Command="{Binding SaveAsFileCommand}"/>
            <Separator/>
            <MenuItem Header="_Exit" Command="{Binding ExitAppCommand}"/>
        </MenuItem>
    </Menu>

但它会产生这样的输出:

Label menuItem output

然后我通过背景图片尝试了它(透明,只有一个纯文本):

    <Menu IsMainMenu="True" Grid.Row="0" Grid.ColumnSpan="3">
        <MenuItem Header="_File">
            <MenuItem.Background>
                <ImageBrush ImageSource="{Binding SelectedObjectResourcePath}"/>
            </MenuItem.Background>
            <MenuItem Header="_Open" Command="{Binding OpenFileCommand}" IsEnabled="{Binding CanOpenFile}"/>
            <Separator/>
            <MenuItem Header="_Save" Command="{Binding SaveFileCommand}"/>
            <MenuItem Header="_Save As" Command="{Binding SaveAsFileCommand}"/>
            <Separator/>
            <MenuItem Header="_Exit" Command="{Binding ExitAppCommand}"/>
        </MenuItem>
    </Menu>

但它只为第一个menuItem设置背景: background imageBrush

1 个答案:

答案 0 :(得分:1)

在我看来,使用StackPanel(水平方向)是最好的解决方案。看一下这个。你只需要让它看起来像你想要的那样好:)

<Menu IsMainMenu="True" Grid.Row="0">
            <MenuItem Header="_File">
                <StackPanel Orientation="Horizontal">
                    <Label Content="Type1" Margin="-30" HorizontalContentAlignment="Center">
                        <Label.LayoutTransform>
                            <RotateTransform Angle="-90"/>
                        </Label.LayoutTransform>
                    </Label>
                    <StackPanel>
                        <MenuItem Header="_Open" Command="{Binding OpenFileCommand}" IsEnabled="{Binding CanOpenFile}"/>
                        <Separator/>
                        <MenuItem Header="_Save" Command="{Binding SaveFileCommand}"/>
                        <MenuItem Header="_Save As" Command="{Binding SaveAsFileCommand}"/>
                        <Separator/>
                        <MenuItem Header="_Exit" Command="{Binding ExitAppCommand}"/>
                    </StackPanel>
                </StackPanel>
            </MenuItem>
        </Menu>