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

时间:2010-12-03 22:08:00

标签: wpf

<WebBrowser x:Name="messageBufferWebBrowser" 
     controls:WebBrowserUtility.Body="{Binding MessageBuilder}"/>

我正在使用此类来启用与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);
            }
        }
    }

}

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

1 个答案:

答案 0 :(得分:6)

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

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

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