简体   繁体   English

WPF菜单项右对齐

[英]WPF Menu item right alignment

I have the following menu. 我有以下菜单。 I am trying to right align the content of the menu. 我正在尝试右对齐菜单的内容。

<Menu Grid.ColumnSpan="3">
        <MenuItem Header="Items">
            <MenuItem>
                <MenuItem.Header>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="Item 1" Grid.Column="0" HorizontalAlignment="Stretch" />
                        <Button Grid.Column="1" Content="E" />
                        <Button Grid.Column="2" Content="D" />
                    </Grid>
                </MenuItem.Header>
            </MenuItem>
            <MenuItem>
                <MenuItem.Header>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="Item 2" Grid.Column="0" HorizontalAlignment="Stretch" />
                        <Button Grid.Column="1" Content="E" />
                        <Button Grid.Column="2" Content="D" />
                    </Grid>
                </MenuItem.Header>
            </MenuItem>
        </MenuItem>
    </Menu>

What is displayed is as below: 显示内容如下:

 ------------------
 Item 1 D E       |
 ------------------
 Item 2 D E       |
 ------------------

There is still lot of space left after E. E之后仍然有很多空间。

What I am expecting is: 我期望的是:

 ------------------
 Item 1       D E |
 ------------------
 Item 2       D E |
 ------------------

I want the D and E to be aligned to the right where as the Item 1 and Item 2 are aligned to the left. 我希望D和E对齐到右边,其中项1和项2对齐到左边。

I tried playing with HorizontalAlignment with no help to find the answer. 我尝试在没有帮助的情况下使用Horizo​​ntalAlignment来寻找答案。

You can use DockPanel instead of Grid for that purpose. 为此,可以使用DockPanel而不是Grid。 Also try to set Width of your menu items explicitly. 另外,尝试显式设置菜单项的宽度。 Like this: 像这样:

    <MenuItem>
        <MenuItem.Header>
            <DockPanel LastChildFill="False" Width="250">
                <TextBlock Text="Item 1" DockPanel.Dock="Left" HorizontalAlignment="Stretch" />
                <Button DockPanel.Dock="Right" Content="E" />
                <Button DockPanel.Dock="Right" Content="D" />
            </DockPanel>
        </MenuItem.Header>
    </MenuItem>

I'm more of a dockpanel type of guy. 我更像是码头工人。 How about you try: 您如何尝试:

<MenuItem>
    <MenuItem.Header>
        <DockPanel LastChildFill="True">
            <Button DockPanel.Dock="Right" Content="E" />
            <Button DockPanel.Dock="Right" Content="D" />

            <TextBlock DockPanel.Dock="Left" Text="Item 1" />
        </DockPanel>
    </MenuItem.Header>
</MenuItem>

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

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