Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 | +-------------------------------------------+ |                                           | 
like image 671
paxdiablo Avatar asked Jun 11 '10 14:06

paxdiablo


People also ask

Which XAML tag is used for menu bar?

The <Menu> and <MenuItem> XAML elements are used to create menus in XAML.

What is a menu WPF?

Advertisements. Menu is a control that enables you to hierarchically organize elements associated with the commands and event handlers. Menu is an ItemsControl, so it can contain a collection of any object type such as string, image, or panel.

How do I create a menu in XAML?

Creating a WPF Menu at Design TimeThe Menu element in XAML creates a menu control. The Name property defines the name of the menu and Height and Width represents the height and width of a menu control. To position a menu control in a Window, the Margin, HorizontalAlignment and VerticalAlignment properties may be used.


2 Answers

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> 
like image 23
Wonko the Sane Avatar answered Sep 20 '22 13:09

Wonko the Sane


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> 
like image 172
Leom Burke Avatar answered Sep 21 '22 13:09

Leom Burke