首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从代码中滚动WPF FlowDocumentScrollViewer?

从代码中滚动WPF FlowDocumentScrollViewer?
EN

Stack Overflow用户
提问于 2009-02-18 13:22:42
回答 5查看 10.3K关注 0票数 10

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

代码语言:javascript
复制
<FlowDocumentScrollViewer Name="Scroller">
 <FlowDocument Foreground="White" Name="docDebug" FontFamily="Terminal">
  <Paragraph Name="paragraphDebug"/>
 </FlowDocument>
</FlowDocumentScrollViewer>

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

有什么建议吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-03-29 14:32:18

这里给出的其他答案有点令人费解,因为我在FlowDocumentScrollViewer上没有看到任何公共的"ScrollViewer“属性。

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

代码语言:javascript
复制
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;
}
票数 8
EN

Stack Overflow用户

发布于 2009-02-18 14:42:24

尝试:

代码语言:javascript
复制
Scroller.ScrollViewer.ScrollToEnd();

其中"Scroller“是您的FlowDocumentScrollViewer的名称。

编辑:我写这个答案有点太快了。FlowDocumentScrollViewer不公开ScrollViewer属性。实际上,我自己扩展了FlowDocumentScrollViewer类并实现了ScrollViewer属性。具体实现如下:

代码语言:javascript
复制
  /// <summary>
  /// Backing store for the <see cref="ScrollViewer"/> property.
  /// </summary>
  private ScrollViewer scrollViewer;

  /// <summary>
  /// Gets the scroll viewer contained within the FlowDocumentScrollViewer control
  /// </summary>
  public ScrollViewer ScrollViewer
  {
     get
     {
        if (this.scrollViewer == null)
        {
           DependencyObject obj = this;

           do
           {
              if (VisualTreeHelper.GetChildrenCount(obj) > 0)
                 obj = VisualTreeHelper.GetChild(obj as Visual, 0);
              else
                 return null;
           }
           while (!(obj is ScrollViewer));

           this.scrollViewer = obj as ScrollViewer;
        }

        return this.scrollViewer;
     }
  }
票数 15
EN

Stack Overflow用户

发布于 2013-03-03 20:39:52

我也遇到过类似的问题:我想要一个文本区域,它可以容纳我的文本,能够包装它,它填充它的父控件,并且是可滚动的。

首先,我尝试将TextBlock与ScrollViewer一起使用,我认为它可以工作,但出于某种原因,我想将FlowDocument与FlowDocumentScrollViewer一起使用。后者不起作用,我就是不能不关注这场战斗,所以我试图找到解决方案,这就是我来到这里的原因。我已经尝试了在原始问题的答案中提供的变通方法,但是这两个解决方案都不适合我(我使用的是.NET 4.5,也许它在其他版本中也有效,但我不知道这是怎么回事)。

我也尝试过单独使用单个FlowDocument,但该控件包含一些我不想要的UI元素。所以,我想出了另一个解决方案。

代码语言:javascript
复制
  <ScrollViewer VerticalScrollBarVisibility="Auto">
    <FlowDocumentScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
      <FlowDocument>

没错。它起作用了!调用ScrollViewer.ScrollToBottom()就行了!ScrollViewer启用滚动,FlowDocumentScrollViewer从FlowDocument中删除UI元素。希望它能帮上忙!

显然,我的结构有一个缺陷,因为这样FlowDocument就不能通过鼠标滚轮滚动。但是,将FlowDocumentScrollViewer控件的IsHitTestVisible属性设置为False可解决此问题。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/561029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档