从代码滚动WPF FlowDocumentScrollViewer? [英] Scroll a WPF FlowDocumentScrollViewer from code?

查看:359
本文介绍了从代码滚动WPF FlowDocumentScrollViewer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个FlowDocumentScrollViewer,我想在添加文本时自动滚动到底部的

I have a FlowDocumentScrollViewer I want to automatically scroll to the bottom when text is added.

<FlowDocumentScrollViewer Name="Scroller">
 <FlowDocument Foreground="White" Name="docDebug" FontFamily="Terminal">
  <Paragraph Name="paragraphDebug"/>
 </FlowDocument>
</FlowDocumentScrollViewer>

在代码中我将Inlines添加到段落中,但是当文本过多时,我会添加
希望能够简单地使用代码向下滚动而不是让用户这样做。

In code I add Inlines to the Paragraph, but when there is to much text I would like to be able to simply scroll down using code instead of having the user doing so.

有任何建议吗?

推荐答案

此处给出的其他答案有些令人困惑,因为在FlowDocumentScrollViewer上没有看到任何公共的 ScrollViewer属性。

The other answers given here are a bit puzzling, since I don't see any public "ScrollViewer" property on the FlowDocumentScrollViewer.

我这样解决了这个问题。请注意,此方法在初始化期间可以返回null:

I hacked around the problem like this. Beware that this method can return null during initialization:

public static ScrollViewer FindScrollViewer(this FlowDocumentScrollViewer flowDocumentScrollViewer)
{
    if (VisualTreeHelper.GetChildrenCount(flowDocumentScrollViewer) == 0)
    {
        return null;
    }

    // Border is the first child of first child of a ScrolldocumentViewer
    DependencyObject firstChild = VisualTreeHelper.GetChild(flowDocumentScrollViewer, 0);
    if (firstChild == null)
    {
        return null;
    }

    Decorator border = VisualTreeHelper.GetChild(firstChild, 0) as Decorator;

    if (border == null)
    {
        return null;
    }

    return border.Child as ScrollViewer;
}

这篇关于从代码滚动WPF FlowDocumentScrollViewer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆