简体   繁体   English

如何在WPF中制作控制基座

[英]How to make a control dock in a wpf

I'm trying to make my menu bar fill the screen horizontally like a menu bar should look. 我试图使菜单栏水平地填充屏幕,就像菜单栏应该看起来一样。 When I run the program the window is set to maximize already but the menu bar only fills half the screen on top. 当我运行程序时,窗口已设置为最大化,但菜单栏仅占顶部屏幕的一半。 I'm not sure how to fix this. 我不确定如何解决此问题。 So just to be clear I'm trying to make my controls fit to appearance based on the size of the window. 因此,为了清楚起见,我试图根据窗口的大小使控件适合外观。

Heres the code: 这是代码:

  <Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Royale" WindowStyle="ThreeDBorderWindow" SizeToContent="Manual" WindowState="Maximized" xmlns:my1="clr-namespace:System;assembly=mscorlib">
<Window.Resources>
    <my1:Double x:Key="Width1">500</my1:Double>
</Window.Resources>
<Grid ShowGridLines="False" Width="Auto">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Menu Height="23" HorizontalAlignment="Left" Name="menu1" VerticalAlignment="Top"       Width="503" IsMainMenu="True" Grid.ColumnSpan="2">
        <MenuItem Header="File" HorizontalContentAlignment="Stretch">
            <MenuItem Header="New">
                <MenuItem Header="New Camper" />
            </MenuItem>
        </MenuItem>
        <MenuItem Header="Edit" />
        <MenuItem Header="View" />
        <MenuItem Header="Add" />
    </Menu>
   </Grid>
   </Window>

Using DockPanel : 使用DockPanel

http://www.wpftutorial.net/DockPanel.html http://www.wpftutorial.net/DockPanel.html

<DockPanel LastChildFill="True">
    <Button Content="Dock=Top" DockPanel.Dock="Top"/>
    <Button Content="Dock=Bottom" DockPanel.Dock="Bottom"/>
    <Button Content="Dock=Left"/>
    <Button Content="Dock=Right" DockPanel.Dock="Right"/>
    <Button Content="LastChildFill=True"/>
</DockPanel>

The simple answer - set your menu to use Grid.ColumnSpan="3" , since you have 3 columns. 简单的答案-将菜单设置为使用Grid.ColumnSpan="3" ,因为您有3列。

However, I would recommend nesting your "main" grid inside of a DockPanel or a second Grid (with 2 rows). 但是,我建议将“主”网格嵌套在DockPanel或第二个网格(具有2行)中。 This way, as you add content (ie: add new columns to your grid), you won't have to constantly adjust your menu to compensate. 这样,当您添加内容(即,向网格中添加新列)时,您将不必不断调整菜单来进行补偿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM