如何创建WPF响应菜单栏(全宽)

时间:2017-07-27 16:08:55

标签: c# wpf

我有一个WPF应用程序,其中启用了WindowState="Maximized"的全屏,但我无法将Menu文档显示为全屏。我从窗口中删除了width属性,但仍然在屏幕的一小部分显示菜单。你可以看看这个,让我知道如何使菜单表现出响应?

<Window x:Class="UMAp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:UBrochure"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <Grid Background="Gray" >
        <Menu x:Name="menu" HorizontalAlignment="Left" Height="28" VerticalAlignment="Top" >
            <Button Content="Edit" BorderThickness="0" Background="#FFF0F0F0"/>
            <Button BorderThickness="0" Content="  Templates" Background="#FFF0F0F0"/>
        </Menu>
        <Grid Background="#FF51514B" HorizontalAlignment="Left" Height="291" Margin="0,28,0,0" VerticalAlignment="Top" Width="134"/>
    </Grid>
</Window>

2 个答案:

答案 0 :(得分:3)

我为实现这一目标所做的工作:

  1. 将菜单的高度和高度设置为自动

  2. 在所有路线上将路线设置为“拉伸”。

  3. 此外,我将网格划分为两列标题和正文,并将第一列高度设置为最大高度,以便在程序全屏时不会变大。

  4. <Grid Background="Gray" >
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition MaxHeight="15" Height="10*"/>
        </Grid.RowDefinitions>
        <Menu x:Name="menu" Height="Auto" Width="Auto" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
            <Button Content="Edit" BorderThickness="0" Background="#FFF0F0F0"/>
            <Button BorderThickness="0" Content="  Templates" Background="#FFF0F0F0"/>
        </Menu>
        <Grid Background="#FF51514B" HorizontalAlignment="Left" Height="291" Margin="0,28,0,0" VerticalAlignment="Top" Width="134" Grid.RowSpan="2"/>
    </Grid>

答案 1 :(得分:0)

更好的解决方案是使用DockPanel代替Grid作为外部容器。设置菜单DockPanel.Dock="Top",它将自动调整其大小,而无需对任何内容进行硬编码。 DockPanel的任何子节点将按照它们在XAML中出现的顺序停靠在窗口的指定侧。如果LastChildFilltrue(默认情况下),则最后一个孩子会自动填满剩余空间。您可以使用此方法创建一些相当复杂的布局。

enter image description here

<Window x:Class="FilterWithBindableCount.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300">
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <Button Content="Edit" BorderThickness="0" Background="#FFF0F0F0"/>
            <Button BorderThickness="0" Content="Templates" Background="#FFF0F0F0"/>
        </Menu>
        <Grid>
            <!--Grid content here-->
        </Grid>
    </DockPanel>
</Window>

成功使用WPF的关键之一是知道所有不同的layout containers是什么,以及何时使用它们。