C# 将UserControl内容拉伸到主窗口大小WPF
我在WPF中有一个用户控件,其中有一个WebBrowser控件。我找不到一种方法来拉伸WebBrowser以填充整个窗口的大小。我尝试了在中找到的所有技巧,但都没有任何帮助。以下是我到目前为止的代码:C# 将UserControl内容拉伸到主窗口大小WPF,c#,wpf,C#,Wpf,我在WPF中有一个用户控件,其中有一个WebBrowser控件。我找不到一种方法来拉伸WebBrowser以填充整个窗口的大小。我尝试了在中找到的所有技巧,但都没有任何帮助。以下是我到目前为止的代码: <UserControl x:Class="TypeAppRelease.controls.HelpUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<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>
如何将主窗口的宽度和高度设置为子用户控件及其子控件的尺寸?我相信默认行为将是
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
对象(如果您不打算重用该控件)
注意:如果您想让用户控件定义大小,您可以从窗口中删除高度
和宽度
属性,并在用户控件中设置它们。您想要的是只覆盖客户端区域还是包括chrome在内的整个窗口?我想要WebBrowser填充整个窗口。