简体   繁体   English

WPF 右对齐菜单项将菜单移动到窗口中心

[英]WPF Right-Aligning Menu Item Moves Menu to Centre of Window

I'm trying to right-align a single menu item though a method found here , but applying this method (seen in the code below) moves the menu to the center of the window, and I can't find an elegant solution to move it back to the top.我试图通过此处找到的方法右对齐单个菜单项,但应用此方法(见下面的代码)将菜单移动到窗口的中心,我找不到一个优雅的解决方案来移动它回到顶部。

<DockPanel>
            <Menu>
                <Menu.ItemsPanel>
                    <ItemsPanelTemplate>
                        <DockPanel/>
                    </ItemsPanelTemplate>
                </Menu.ItemsPanel>
                <MenuItem Header="File">
                    <MenuItem Header="Home" Click="MenuItem_Home_Click"/>
                    <MenuItem Header="About"/>
                    <MenuItem Header="Settings" Click="MenuItem_Settings_Click"/>
                    <Separator/>
                    <MenuItem Header="Exit" Click="MenuItem_Exit_Click"/>
                </MenuItem>
                <MenuItem Header="Notifications" HorizontalAlignment="Right" FlowDirection="RightToLeft"/>
            </Menu>
</DockPanel>

Menu location with code带代码的菜单位置

Don't suppose anyone can work out what I've done wrong here?不要假设任何人都可以解决我在这里做错了什么?

I've tried various different methods to fix this, only removing the whole Menu.ItemsPanel code works in returning the menu to it's original location, but that also moves the Notifications menu item to the left.我已经尝试了各种不同的方法来解决这个问题,只有删除整个 Menu.ItemsPanel 代码才能将菜单返回到它的原始位置,但也会将通知菜单项移到左侧。

You need to put it in a container, such as a grid, that restricts it's height.您需要将它放在限制其高度的容器中,例如网格。


<Grid>

  <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
  </Grid.RowDefinitions>

  <Menu Grid.Row="0">
    <Menu.ItemsPanel>
        <ItemsPanelTemplate>
            <DockPanel HorizontalAlignment="Stretch"/>
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>
    <MenuItem Header="File">
        <MenuItem Header="Home" Click="MenuItem_Home_Click"/>
        <MenuItem Header="About"/>
        <MenuItem Header="Settings" Click="MenuItem_Settings_Click"/>
        <Separator/>
        <MenuItem Header="Exit" Click="MenuItem_Exit_Click"/>
    </MenuItem>
    <MenuItem Header="Notifications" HorizontalAlignment="Right" FlowDirection="RightToLeft"/>
  </Menu>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM