WPF 工具栏中的下拉菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1790858/
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 21:15:01  来源:igfitidea点击:

Dropdown Menu in WPF Toolbar

wpfmenutoolbar

提问by egoodberry

Having some layout frustrations in WPF- I'm using a ToolBar to house a set of controls, most of which are Buttons and one of which is (going to be) some sort of dropdown menu. In WinForms, the ToolStripDropDownButton was perfect; however, I can't seem to figure out the best way to replicate this behavior in WPF.

在 WPF 中遇到一些布局问题 - 我使用 ToolBar 来容纳一组控件,其中大部分是按钮,其中一个是(将是)某种下拉菜单。在 WinForms 中,ToolStripDropDownButton 是完美的;但是,我似乎无法找出在 WPF 中复制此行为的最佳方法。

Any ideas?

有任何想法吗?

回答by gbc

You could try placing a Menu & MenuItem inside the Toolbar. I've had to use Menu's and MenuItem trees in various parts of the interface (besides classical menus) to get the dropdown menu behavior. You can tweak the control template of the menu to sculpt the look and feel to look however you like and completely abandon the vanilla menu look and feel.

您可以尝试在工具栏中放置一个菜单和菜单项。我不得不在界面的各个部分(除了经典菜单)使用 Menu 和 MenuItem 树来获取下拉菜单行为。您可以调整菜单的控制模板来塑造您喜欢的外观和感觉,并完全放弃香草菜单的外观和感觉。

Here's some XAML to show a simple implementation:

下面是一些 XAML 来展示一个简单的实现:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
    <ToolBar>
            <Button Content="Button1"></Button>
            <Button Content="Button2"></Button>
            <Menu>
                <MenuItem Header="Menu">
                    <MenuItem Header="MenuItem1"/>
                </MenuItem>
            </Menu>
    </ToolBar>
</StackPanel>