在 Wpf 中创建垂直菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7932060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:09:31  来源:igfitidea点击:

Create a vertical Menu in a Wpf

wpfxaml

提问by arjacsoh

How is it possible to create a vertical menu on the left side of the window in Visual Studio (in a wpf) with xaml like the one in http://www.wpftutorial.net/? I try the code:

如何使用 xaml 在 Visual Studio(在 wpf 中)的窗口左侧创建垂直菜单,就像http://www.wpftutorial.net/ 中的那样?我尝试代码:

<Menu DockPanel.Dock="Left" VerticalAlignment="Top" Background="Gray" BorderBrush="Black">

but it does not the task, since it presents a horizontal menu on the top.

但它不是任务,因为它在顶部显示了一个水平菜单。

It is not required to be done definitely by the control menu. If any other control with similar appearance is appropriate, it is acceptable.

不需要通过控制菜单来确定。如果任何其他外观相似的控件是合适的,则可以接受。

回答by Rachel

Sure, just change MenuItem.ItemsPanelto use a Vertical StackPanel instead of the Default Horizontal one

当然,只需更改MenuItem.ItemsPanel为使用 Vertical StackPanel 而不是 Default Horizo​​ntal 一个

<Menu>
    <Menu.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>

</Menu>

回答by bto.rdz

Accepted anwer is good. But it is a long way to get a good side bar working. you can use this control if what you need is a working menu now.

接受的答案是好的。但要让侧边栏发挥作用,还有很长的路要走。如果您现在需要的是一个工作菜单,您可以使用此控件。

https://github.com/beto-rodriguez/MaterialMenu

https://github.com/beto-rodriguez/MaterialMenu

you can install it from nuget too.

您也可以从 nuget 安装它。

here is an example

这是一个例子

<materialMenu:SideMenu HorizontalAlignment="Left" x:Name="Menu"
                           MenuWidth="300"
                           Theme="Default"
                           State="Hidden">
        <materialMenu:SideMenu.Menu>
            <ScrollViewer VerticalScrollBarVisibility="Hidden">
                <StackPanel Orientation="Vertical">
                    <Border Background="#337AB5">
                        <Grid Margin="10">
                            <TextBox Height="150" BorderThickness="0" Background="Transparent"
                                VerticalContentAlignment="Bottom" FontFamily="Calibri" FontSize="18"
                                Foreground="WhiteSmoke" FontWeight="Bold">Welcome</TextBox>
                        </Grid>
                    </Border>
                    <materialMenu:MenuButton Text="Administration"></materialMenu:MenuButton>
                    <materialMenu:MenuButton Text="Packing"></materialMenu:MenuButton>
                    <materialMenu:MenuButton Text="Logistics"></materialMenu:MenuButton>
                </StackPanel>
            </ScrollViewer>
        </materialMenu:SideMenu.Menu>
    </materialMenu:SideMenu>

回答by Mark Adamantine

You can adjust the ItemsPanel using Style (which I feel is much more wpf-ish)

您可以使用 Style 调整 ItemsPanel(我觉得它更像 wpf-ish)

<Window.Resources>
        <Style TargetType="Menu" x:Key="Horizontal">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <StackPanel VerticalAlignment="Center"/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>
<Window.Resources>

the VerticalAlignment="Center"is there for beautification purposes, you can definitely skip it.

VerticalAlignment =“中心”是有美化的目的,你绝对可以跳过它。

then in the menu code

然后在菜单代码中

<Menu Style="{StaticResource Horizontal}">
    ...
</Menu>