如何填写DockPanel?

时间:2019-05-18 14:11:47

标签: c# .net wpf

我有这个.xaml代码:

<Window x:Class="Eagle_Eye.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:Eagle_Eye"
        mc:Ignorable="d"
        ResizeMode="NoResize"
        Title="EE" Height="450" Width="800">
    <Grid>
        <DockPanel>
            <StackPanel Orientation="Vertical" DockPanel.Dock="Top" Margin="0" >
            </StackPanel>
            <StackPanel Orientation="Vertical" DockPanel.Dock="Top"  >
                <TextBlock Name="debugText" Height="20" Background="Azure" Foreground="Red"></TextBlock>
            </StackPanel>



        <WebBrowser Name="webBrowser" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></WebBrowser>
        </DockPanel>



    </Grid>
</Window>

.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Eagle_Eye
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            debugText.Text = "hello";
            webBrowser.Navigate("https://www.google.com");
        }
    }
}

这给我的是这样:

enter image description here

仅占用屏幕的一半,并且有滚动条。我想念什么?

这是我想要的:

enter image description here

*黄色框应包含TextBlock,蓝色框应包含WebBrowser,且无滚动条。 *

我该如何实现?

1 个答案:

答案 0 :(得分:1)

您在这里遇到几个问题, 首先,Dockpanel默认情况下具有“ LastChildFill”选项,这意味着最后一个子项将填充面板的其余部分,如果将​​最后一个子项绑定到底部,它将不会拉伸。 其次,堆栈面板项目不会拉伸,并且始终要占用最小的空间。

所以我要做的是替换:

        <StackPanel Orientation="Vertical" DockPanel.Dock="Bottom">
            <WebBrowser Name="webBrowser"></WebBrowser>
        </StackPanel>

与此:

            <WebBrowser Name="webBrowser" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></WebBrowser>