如何在 WPF 中右对齐“帮助”菜单项?

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

How do I right-align the 'help' menu item in WPF?

wpfxamlvisual-studio-2008

提问by paxdiablo

I have the following (simplifed) section in my XAML file:

我的 XAML 文件中有以下(简化的)部分:

<Menu Width="Auto" Height="20" Background="#FFA9D1F4" DockPanel.Dock="Top">
    <MenuItem Header="File">
        <MenuItem Header="Exit"/>
    </MenuItem>
    <MenuItem Header="Edit">
        <MenuItem Header="Cut"/>
    </MenuItem>
    <MenuItem Header="Help">
        <MenuItem Header="About"/>
    </MenuItem>
</Menu>

and it results in:

结果是:

+-------------------------------------------+
| File Edit Help                            |
+-------------------------------------------+
|                                           |

What do I need to do if I want the Helpmenu item on the right-hand side:

如果我想要Help右侧的菜单项,我需要做什么:

+-------------------------------------------+
| File Edit                            Help |
+-------------------------------------------+
|                                           |

回答by Leom Burke

Alng the same principle and this time you dont need the grid and therefore dont need to know the number of items. Assign all items to the left except the help :)

同样的原理,这次你不需要网格,因此不需要知道项目的数量。将除帮助外的所有项目都分配到左侧:)

<Menu Height="20" Background="#FFA9D1F4">
    <Menu.ItemsPanel>
        <ItemsPanelTemplate>
            <DockPanel HorizontalAlignment="Stretch"/>
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>
    <MenuItem Header="File">
        <MenuItem Header="Exit"/>
    </MenuItem>
    <MenuItem Header="Edit">
        <MenuItem Header="Cut"/>
    </MenuItem>
    <MenuItem Header="Help" HorizontalAlignment="Right">
        <MenuItem Header="About"/>
    </MenuItem>
</Menu>

回答by Wonko the Sane

Another possible answer, if you always know how many menu items there will be (and that makes this answer fragile), is to define the Menu.ItemsPanel as a grid, set the Menu to Stretch, set the Grid.ColumnDefinitions appropriately, set the MenuItems to the appropriate Grid.Column, and set the HorizontalAlignment of the last menu item as Right.

另一个可能的答案,如果你总是知道会有多少菜单项(这使得这个答案很脆弱),就是将 Menu.ItemsPanel 定义为网格,将 Menu 设置为 Stretch,适当地设置 Grid.ColumnDefinitions,设置MenuItems 到适当的 Grid.Column,并将最后一个菜单项的 Horizo​​ntalAlignment 设置为 Right。

   <Menu  Height="20" Background="#FFA9D1F4" DockPanel.Dock="Top" HorizontalAlignment="Stretch">
        <Menu.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </Menu.ItemsPanel>
        <MenuItem Header="File" Grid.Column="0">
            <MenuItem Header="Exit"/>
        </MenuItem>
        <MenuItem Header="Edit" Grid.Column="1">
            <MenuItem Header="Cut"/>
        </MenuItem>
        <MenuItem Header="Help" Grid.Column="2" HorizontalAlignment="Right">
            <MenuItem Header="About"/>
        </MenuItem>
    </Menu>

回答by Pete Bosch

Alteration to the original answer, since as it is (with HorizontalAlignment="Stretch") the Edit menu will stretch all the way across the menu bar to the Help menu.

更改原始答案,因为它是(使用 Horizo​​ntalAlignment="Stretch")编辑菜单将一直延伸到菜单栏到帮助菜单。

Also incorporating Rokke's submenu alignment suggestion...

还结合了 Rokke 的子菜单对齐建议......

<Menu Height="20" Background="#FFA9D1F4">
        <Menu.ItemsPanel>
            <ItemsPanelTemplate>
                <DockPanel/>
            </ItemsPanelTemplate>
        </Menu.ItemsPanel>
        <MenuItem Header="File">
            <MenuItem Header="Exit"/>
        </MenuItem>
        <MenuItem Header="Edit">
            <MenuItem Header="Cut"/>
        </MenuItem>
        <MenuItem Header="_Help" HorizontalAlignment="Right" FlowDirection="RightToLeft">
            <MenuItem Header="About..." FlowDirection="LeftToRight"/>
        </MenuItem>
    </Menu>

回答by Rokke

<Menu DockPanel.Dock="Top">
  <Menu.ItemsPanel>
    <ItemsPanelTemplate>
      <DockPanel HorizontalAlignment="Stretch"/>
    </ItemsPanelTemplate>
  </Menu.ItemsPanel>
  <MenuItem Header="_File">
    <MenuItem Click="Exit_Clicked" Header="E_xit" />
  </MenuItem>
  <MenuItem Header="_User">
    <MenuItem Click="ChangePassword_Clicked" Header="_Change Password..." />
  </MenuItem>
  <!-- Right adjusted help menu with sub menu keeping inside the main window -->
  <MenuItem Header="_Help" HorizontalAlignment="Right" FlowDirection="RightToLeft">
    <MenuItem Click="About_Clicked" Header="_About..." FlowDirection="LeftToRight" />
  </MenuItem>
</Menu>

Just a small addition to the original answer. Including the two flow directions make the sub menu stay inside the window.

只是对原始答案的一个小补充。包括两个流向使子菜单留在窗口内。

回答by Marks

I don't think there is an easy way. Menu keeps all Items on one side and even ignores HorizontalContentAlignment of the Menu or HorizontalAlignment of the MenuItem.

我认为没有简单的方法。Menu 将所有项目保留在一侧,甚至忽略 Menu 的 Horizo​​ntalContentAlignment 或 MenuItem 的 Horizo​​ntalAlignment。

But you could do a workaround. The margin property works. So i think you could bind the margin of the Help MenuItem to the width of the Menu. But you would have to use a Converter to calculate the margin from the width.

但是你可以做一个解决方法。边距属性有效。所以我认为你可以将帮助菜单项的边距绑定到菜单的宽度。但是您必须使用转换器来计算宽度的边距。

I dont know if its good to do something like that. I wouldn't. But if you really want it, thats a way that should work.

我不知道做这样的事情是否好。我不会。但如果你真的想要它,那应该是一种可行的方式。

回答by Zev Spitz

There are one, perhaps two, potential drawbacks to using either a DockPanel or a Grid, instead of the default WrapPanel.

使用 DockPanel 或 Grid 而不是默认的 WrapPanel 有一个,也许有两个潜在的缺点。

  1. If the window is too small to show all the menu items on one line, they won't wrap to the next.
  2. If the menuitems will be shared for use in a different context, such as in a ContextMenu, the last item will be right-aligned
  1. 如果窗口太小而无法在一行中显示所有菜单项,它们将不会换行到下一行。
  2. 如果将共享菜单项以在不同的上下文中使用,例如在 ContextMenu 中,则最后一项将右对齐