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

标签 wpf

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

我正在使用这个类来启用绑定(bind)到 WebBrowser 控件的正文
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,我将它绑定(bind)到 ViewModel 上的 StringBuilder 属性。如何让 WebBrowser 控件滚动到末尾?

最佳答案

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

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

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

关于wpf - 如何将 WPF WebBrowser 控件滚动到最后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4350277/

相关文章:

WPF xmlns :local="clr-namespace:

.net - 为按钮使用 StyleSelector

.net - 如何在 WPF 中创建基页?

c# - 通用 View 模型编辑支持

c# - 如何为 GroupBox 标题使用样式?

c# - WPF 自定义控件 - 绑定(bind)到代码隐藏中定义的命令

wpf - 为什么在 Collection 更改时不调用 Converter?

c# - 如何使 WPF 窗口位于我的应用程序的所有其他窗口之上(不是系统范围的)?

c# - 如何在 WPF 中创建一组单选菜单项?

c# - "ClickOnce does not support the request execution level ' 需要管理员。 '"