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 | +-------------------------------------------+ | |
The <Menu> and <MenuItem> XAML elements are used to create menus in XAML.
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.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With