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

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


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>


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


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.

Also incorporating Rokke's submenu alignment suggestion...

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