C# 如何创建垂直菜单并在菜单上创建子菜单';WPF的右边是什么?
如图所示,如何在WPF中创建这样的菜单C# 如何创建垂直菜单并在菜单上创建子菜单';WPF的右边是什么?,c#,wpf,C#,Wpf,如图所示,如何在WPF中创建这样的菜单 我尝试过使用弹出控件和菜单控件,但效果并不理想尝试使用MenuItem.ItemsPanel 比如说 <Menu> <Menu.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Vertical"/> </ItemsPanelTemplate> </Menu.ItemsP
我尝试过使用弹出控件和菜单控件,但效果并不理想尝试使用
MenuItem.ItemsPanel
比如说
<Menu>
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
</Menu>
尝试使用
MenuItem.ItemsPanel
比如说
<Menu>
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
</Menu>
只需使用弹出菜单控件即可。无需更改项目面板。只需使用弹出菜单控件即可。无需更改项目面板。您可以使用切换按钮和弹出窗口实现此菜单
<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>
输出
您可以使用切换按钮和弹出窗口实现此菜单
<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>
输出
你必须做两件事:
<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>
结果是:
你必须做两件事:
<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>
结果是:
另一种可能是使用扩展控件。我尝试过设置,它可以使菜单垂直,但不能使子菜单位于右侧。另一种可能是使用扩展控件。我尝试过设置,它可以使菜单垂直,但不能使子菜单位于右侧。弹出菜单控件是什么?是关于DevExpress的吗?哎呀。很抱歉ContextMenu类()。什么是PopupMenu控件?是关于DevExpress的吗?哎呀。很抱歉ContextMenu类()。这正是我想要的!非常感谢你!还有一个问题,当我从上下文菜单中选择EditTemplate时,它会自动生成许多行代码。我只想更改弹出窗口的位置值,我不希望Xaml包含很多行代码。我如何简单地做到这一点?非常感谢。请你再详细说明一下。我看不到任何弹出位置值。这没有意义。您需要编辑菜单项模板。在我的代码中,它是在样式MenuItemStyle1中定义的,我没有包括它,因为它可以由visual studio生成。谷歌wpf编辑template@Liero我使用了你的代码,当长单词-文本部分隐藏。知道为什么吗?这正是我想要的!非常感谢你!还有一个问题,当我从上下文菜单中选择EditTemplate时,它会自动生成许多行代码。我只想更改弹出窗口的位置值,我不希望Xaml包含很多行代码。我如何简单地做到这一点?非常感谢。请你再详细说明一下。我看不到任何弹出位置值。这没有意义。您需要编辑菜单项模板。在我的代码中,它是在样式MenuItemStyle1中定义的,我没有包括它,因为它可以由visual studio生成。谷歌wpf编辑template@Liero我使用了你的代码,当长单词-文本部分隐藏。你知道为什么吗?