0
votes

I have a user control in WPF which has a WebBrowser control in it.I can't find a way how to stretch the WebBrowser to fill the size of the whole window.I tried all sort of tricks I found here in SO but nothing helped.Here is my code so far :

<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>

How can I set main window's width and height to be the dimensions of the child user control and of it's children?

1
are you wanting just the client area covered or the entire window including chrome?Mark Hall
I want the WebBrowser to fill the whole window.Michael IV

1 Answers

1
votes

I believe the default behaviour will be for the WebBrowser to fill the UserControl; depending on how you then use your UserControl, it should expand to fill your window, if required.

Adding the Source property to the WebBrowser will give you a better idea of the space it's currently occupying.

This control:

<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>

Should expand to fill this window:

<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>

I've removed the row and column definitions, as there weren't yet any additional objects other than the WebBrowser, I'm assuming you'll stick some more in, otherwise you could just use the WebBrowser object directly in your Window (if you aren't intending on reusing the control).

N.B. you could remove the Height and Width properties from the Window, and set thiem in the UserControl, if you wanted it to define the size.