WPF: How to adjust the size of a WebBrowser inside a popup?

I have a Popupc WebBrowserinside (see code below). WebBrowser has one MaxWidth = "800". I want to automatically adjust the height of the web browser to its content height after loading the website, so it does not need a vertical ScrollBar.

I tried installing Height = "Auto"or MaxHeight = "5000", but I am not getting the correct result.

Can you help me how to do this? Thank you very much!

    <Popup Name="popup1" VerticalOffset="3">
        <Border BorderThickness="1">
            <DockPanel>
                <ScrollViewer MaxHeight="700" VerticalScrollBarVisibility="Auto">
                    <WebBrowser Name="wb1" MaxWidth="800"/>
                </ScrollViewer>
            </DockPanel>
        </Border>
    </Popup>
+1
source share
1 answer

Here is the solution:

<Popup x:Name="Popup" Width="800">
    <Border BorderThickness="1">
        <WebBrowser
            x:Name="WebBrowser"
            LoadCompleted="WebBrowser_OnLoadCompleted"
            ... />
    </Border>
</Popup>

Handler:

private void WebBrowser_OnLoadCompleted(object sender, NavigationEventArgs e) {
    this.Popup.Height = (int) (this.WebBrowser.Document as dynamic).body.scrollHeight + 20;
}
+2
source

Source: https://habr.com/ru/post/1662320/


All Articles