wpf 如何在WPF中创建垂直菜单并在菜单右侧制作子菜单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31977828/
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
How to create the vertical menu and make the submenu on the menu's right side in WPF?
提问by Prince
回答by Liero
You have to do two things:
你必须做两件事:
Set Menu.ItemsPanel to StackPanel
Change the Popup's Placementof MenuItems from Bottom to Right. I just right-clicked on the MenuItem in visual studio designer and picked EditTempalate from context menu. In the template I found popup control and changed the placement to Right. It works nice
将 Menu.ItemsPanel 设置为 StackPanel
将弹出的菜单项的位置从底部更改为右侧。我只是在 Visual Studio 设计器中右键单击 MenuItem,然后从上下文菜单中选择 EditTempalate。在模板中,我找到了弹出控件并将位置更改为 Right。效果很好
Final xaml:
最终的xaml:
<Menu HorizontalAlignment="Left">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="Item 1" />
<MenuItem Header="Item 2" Style="{StaticResource MenuItemStyle1}">
<MenuItem Header="Sub item 1" />
<MenuItem Header="Sub item 2" />
<MenuItem Header="Sub item 3" />
<MenuItem Header="Sub item 4" />
</MenuItem>
<MenuItem Header="Item 3" />
<MenuItem Header="Item 4" />
</Menu>
And the result:
结果:
回答by Heena Patil
You can implement this menu using ToggleButton and Popup.
您可以使用 ToggleButton 和 Popup 来实现此菜单。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="500"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ToggleButton Content="Home" Name="ToggleButton1" Foreground="Black" Focusable="false" IsChecked="{Binding Path=IsOpen,Mode=TwoWay,ElementName=Popup1}" ClickMode="Press"/>
<ToggleButton Content="Controls" Grid.Row="1" Name="ToggleButton2" Foreground="Black" Focusable="false" IsChecked="{Binding Path=IsOpen,Mode=TwoWay,ElementName=Popup2}" ClickMode="Press"/>
<Popup VerticalAlignment="Top" PlacementTarget="{Binding ElementName=ToggleButton1}" Grid.Column="1" HorizontalAlignment="Left" Name="Popup1" Placement="Right" IsOpen="False" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid Background="Gray" HorizontalAlignment="Left" VerticalAlignment="Stretch" SnapsToDevicePixels="True" Width="300" Height="300">
<StackPanel HorizontalAlignment="Stretch" Background="Transparent">
<TextBlock Text="Xaml"></TextBlock>
<TextBlock Text="Routed Events"></TextBlock>
<TextBlock Text="Visual Tree"></TextBlock>
</StackPanel>
</Grid>
</Popup>
<Popup VerticalAlignment="Top" PlacementTarget="{Binding ElementName=ToggleButton2}" Grid.Column="1" HorizontalAlignment="Left" Name="Popup2" Placement="Right" IsOpen="False" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid Background="Green" HorizontalAlignment="Left" VerticalAlignment="Stretch" SnapsToDevicePixels="True" Width="300" Height="300">
<StackPanel HorizontalAlignment="Stretch" Background="Transparent">
<TextBlock Text="Xaml"></TextBlock>
<TextBlock Text="Routed Events"></TextBlock>
<TextBlock Text="Visual Tree"></TextBlock>
</StackPanel>
</Grid>
</Popup>
</Grid>
回答by kulotskie
try to use MenuItem.ItemsPanel
尝试使用 MenuItem.ItemsPanel
for example
例如
<Menu>
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
</Menu>
回答by SledgeHammer
Just use the PopupMenu control man. No need for changing the items panel.
只需使用 PopupMenu 控制人员即可。无需更改项目面板。


