问题描述:

How can I make the menu bar in the WPF Application's xaml file to resize itself automatically if I maximize or minimize the window?

Here is the code:

<Window x:Class="Interface.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Physiosoft" Height="750" Width="1100">

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto" />

<ColumnDefinition Width="*" />

<ColumnDefinition Width="Auto" />

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="*"/>

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

<Menu Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" Height="23" HorizontalAlignment="Left" Name="menu1" VerticalAlignment="Top" Width="1088" MinHeight="23" MinWidth="1088">

<MenuItem Header="File">

<MenuItem Header="Save Ctrl + S" />

</MenuItem>

<MenuItem Header="Help">

<MenuItem Header="About Physiosoft F1" />

</MenuItem>

</Menu>

</Grid>

网友答案:

Either use it inside a DockPanel with its property of docking to fill (by, precisely not setting it) or within a Grid column/row whose width/height is set to * so it will auto resize to the available space.

This, as example, will create a three column layout with a main panel and a resizable menu, use it as an start point:

<Window x:Class="Interface.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Physiosoft" Height="750" Width="1100">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Menu Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1"  Name="menu1">
        <MenuItem Header="File">
            <MenuItem Header="Save                     Ctrl + S" />
        </MenuItem>
        <MenuItem Header="Help">
            <MenuItem Header="About Physiosoft                                F1" />
        </MenuItem>
    </Menu>
</Grid>
相关阅读:
Top