wpfxamlvisual-studio-2008

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


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

<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 Help menu item on the right-hand side:

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

Solution

  • 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>