如何在WPF中使用正确的对齐菜单填充顶部

时间:2015-01-07 20:25:08

标签: wpf menu alignment dock

我是WPF的新手。我想要一个正确的对齐菜单。但是当将horizo​​ntalalignment属性设置为right时,它不会填充整行的宽度。我使用horizo​​ntalcontentalignment作为拉伸,但它不起作用。这是我的代码:

    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
      </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="60" />
        <RowDefinition Height="*" />
        <RowDefinition />
    </Grid.RowDefinitions>


    <DockPanel Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right">
        <Menu DockPanel.Dock="Top" >
            <MenuItem Header="_File"/>
            <MenuItem Header="_Edit"/>
            <MenuItem Header="_Help"/>
        </Menu> 
     </DockPanel>
</Grid>

显示的内容如下:

    -----------------------------
    File edit help              |
    -----------------------------

但我想成为:

   ---------------------------------
                     Help edit file|
    --------------------------------

如何获得菜单填充顶部并将其对齐设置为正确?

1 个答案:

答案 0 :(得分:2)

你可以这样做:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

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

    <Menu Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch">
        <Menu.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" VerticalAlignment="Top" />
            </ItemsPanelTemplate>
        </Menu.ItemsPanel>

        <MenuItem Header="_File" />
        <MenuItem Header="_Edit"/>
        <MenuItem Header="_Help"/>
    </Menu>
</Grid>