如何将WPF WebBrowser控件滚动到最后?

时间:2022-09-24 19:45:11
<WebBrowser x:Name="messageBufferWebBrowser" 
     controls:WebBrowserUtility.Body="{Binding MessageBuilder}"/>

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

我正在使用此类来启用绑定到WebBrowser控件的Body

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?

这是我的WebBrowser,我将它绑定到ViewModel上的StringBuilder属性。如何让WebBrowser控件滚动到最后?

1 个解决方案

#1


6  

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

如果将WebBrowser的Document属性强制转换为mshtml.HTMLDocument,则可以滚动到页面中的特定位置(或使用可能的最大值滚动到底部):

var html = webBrowser.Document as mshtml.HTMLDocument;
html.parentWindow.scroll(0, 10000000);

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

请注意,您必须在项目中添加对Microsoft.mshtml的引用。

#1


6  

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

如果将WebBrowser的Document属性强制转换为mshtml.HTMLDocument,则可以滚动到页面中的特定位置(或使用可能的最大值滚动到底部):

var html = webBrowser.Document as mshtml.HTMLDocument;
html.parentWindow.scroll(0, 10000000);

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

请注意,您必须在项目中添加对Microsoft.mshtml的引用。