HOW DO I SCROLL A WPF WEBBROWSER CONTROL TO THE END?

ezra heywood

Total Post:145

Points:1019
Posted by  ezra heywood
WPF 
WPF
 5401  View(s)
Ratings:
Rate this:
<WebBrowser x:Name="messageBufferWebBrowser"

     controls:WebBrowserUtility.Body="{Binding MessageBuilder}"/>

I'm using this class to enable binding to the Body of the WebBrowser control

public static class WebBrowserUtility

{

    public static readonly DependencyProperty BodyProperty =

    DependencyProperty.RegisterAttached("Body", typeof(string), typeof(WebBrowserUtility), new PropertyMetadata(OnBodyChanged));

    public static string GetBody(DependencyObject dependencyObject)

    {

        return (string)dependencyObject.GetValue(BodyProperty);

    }

    public static void SetBody(DependencyObject dependencyObject, string body)

    {

        dependencyObject.SetValue(BodyProperty, body);

    }

    private static void OnBodyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

    {

        var webBrowser = d as WebBrowser;

        if (!string.IsNullOrWhiteSpace(e.NewValue as string) && webBrowser != null)

        {

            if (Application.Current.MainWindow != null && !DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow))

            {

                webBrowser.NavigateToString((string)e.NewValue);

            }

        }

    }

}

That's my WebBrowser, I'm binding it to a StringBuilder property on the ViewModel. How can I get the WebBrowser control to scroll to the end?

  1. Vijay Shukla

    Post:100

    Points:700
    Re: How do I scroll a WPF WebBrowser control to the end?

    If you cast the WebBrowser's Document property to a mshtml.HTMLDocument, then you can scroll to a specific position in the page (or the bottom by using the largest value possible):

    var html = webBrowser.Document as mshtml.HTMLDocument;

    html.parentWindow.scroll(0, int.MaxValue);

    Note you have to add a reference to Microsoft.mshtml in your project.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!