2013-06-23 56 views
0

我在WPF中有一个用户控件,它有一个WebBrowser控件。我无法找到如何拉伸WebBrowser以填充整个窗口大小的方法。我尝试了所有那种招数我发现这里的SO,但没有helped.Here是到目前为止我的代码:将用户控件内容拉伸到主窗口大小WPF

<UserControl x:Class="TypeAppRelease.controls.HelpUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" Background="Aqua" 
     VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" Loaded="UserControl_Loaded_1" Margin="0"> 
<Grid Name="parentGrid"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 

    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 

    </Grid.ColumnDefinitions> 

     <WebBrowser Name="browser" HorizontalAlignment="Center" VerticalAlignment="Stretch" /> 

    </Grid> 
</UserControl> 

如何设置主窗口的宽度和高度是孩子的用户控件的尺寸和它的孩子?

+0

你想要的客户区覆盖或整个窗口包括铬? –

+0

我想让WebBrowser填满整个窗口。 –

回答

1

我相信默认行为将为WebBrowser填充UserControl;取决于你如何使用你的UserControl,它应该扩大到填满你的窗口,如果需要的话。

Source属性添加到WebBrowser会让您更好地了解它当前占据的空间。

这种控制:

<UserControl x:Class="WpfApplication1.UserControl1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" Background="Aqua" Margin="0"> 
    <Grid Name="parentGrid"> 
     <WebBrowser Name="browser" Source="http://stackoverflow.com" /> 
    </Grid> 
</UserControl> 

应该扩大,以填补这个窗口:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApplication1" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <local:UserControl1 /> 
</Grid> 

我已经删除的行和列的定义,有没有任何额外的除了WebBrowser之外的其他对象,我假设您会继续添加更多内容,否则您可以直接在您的中直接使用WebBrowser对象(如果您不打算重新使用该控件)。

N.B.您可以从Window中删除HeightWidth属性,并在UserControl中设置thiem,如果您希望它定义大小。