I'm trying to understand how to add a Menu
to my WPF XAML window. My window currently uses a Grid
for content layout. I'd like to add a Menu
at the top of the window, similar to how you usually see in WinForm applications.
I've looked around at examples, but none of the examples seem to explain the use a Grid
. I see most examples using things like a StackPanel
, or DockPanel
.
Here's my current Window
, with the Grid
. Should I just give this Menu
its own row, or do I need some sort of panel?
<Window>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="3*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<Menu>
<MenuItem Header="File" />
</Menu>
<GroupBox Header="Seasons" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2">
<DataGrid Name="lstSeasons" AutoGenerateColumns="False" IsReadOnly="True" HeadersVisibility="Column" ItemsSource="{Binding SeasonsCollectionView}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}" />
<DataGridTextColumn Header="Division" Width="*" Binding="{Binding Division}" />
</DataGrid.Columns>
</DataGrid>
</GroupBox>
</Grid>
</Window>
You can add Menu
to the main WPF layout Grid
as shown in the following example:
<Menu Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0">
<MenuItem Header="_File"/>
<MenuItem Header="_Edit"/>
<MenuItem Header="_Help"/>
</Menu>
You do not need any other container controls in order to complete this task. The screenshot of the actual WPF app using this Menu
technique is shown here (notice the Menu
Bar at the top of the app window): http://examn8.com
Hope this may help.
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