2009-02-18 24 views
6

我有一個FlowDocumentScrollViewer,我想在添加文本時自動滾動到底部 。從代碼中滾動WPF FlowDocumentScrollViewer?

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

在代碼中我添加內聯段落,但是當有太多的文字我 希望能夠簡單地向下滾動使用代碼,而不是讓用戶這樣做。

有什麼建議嗎?

回答

6

這裏給出其他的答案有點令人費解,因爲我沒有看到關於FlowDocumentScrollViewer任何公開「的ScrollViewer」屬性。

我繞過這樣的問題。要注意的是這種方法可以在初始化時返回null:

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; 
} 
+0

感謝您指出我的答案中的錯誤。我糾正了它。 – 2009-04-01 18:21:13

12

嘗試:

Scroller.ScrollViewer.ScrollToEnd(); 

其中, 「滾輪」 是你FlowDocumentScrollViewer的名稱。

編輯:我寫這個答案有點太快了。 FlowDocumentScrollViewer不公開ScrollViewer屬性。我實際上擴展了FlowDocumentScrollViewer類並自己實現了ScrollViewer屬性。下面是執行:

/// <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; 
    } 
    } 
+0

我得到和錯誤,這不能轉換爲DependencyObject – Paparazzi 2013-05-08 22:55:09

9

我已經遇到了類似的問題:我想要一個文本區域可以存放我的文字,是能包住它,它填補了其母公司的控制和是可滾動的。

首先,我試圖用的TextBlock的ScrollViewer,我認爲它的工作,但由於某種原因,我想用一個的FlowDocument而不是用FlowDocumentScrollViewer。後者沒有奏效,我只是無法離開戰鬥,所以我試圖找到解決方案,這就是我如何到達這裏。我試圖將解答中提出的解決方法應用於原始問題,但是這兩種解決方案都不適合我(我使用的是.NET 4.5,也許它適用於其他版本,但我不知道這一點)。

我已經嘗試過使用單獨的FlowDocument本身,但該控件包含一些我不想要的UI元素。所以,我想出了另一個解決方案。

<ScrollViewer VerticalScrollBarVisibility="Auto"> 
    <FlowDocumentScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"> 
     <FlowDocument> 

沒錯。有用!調用ScrollViewer.ScrollToBottom()只是工作! ScrollViewer啓用滾動和FlowDocumentScrollViewerFlowDocument中刪除UI元素。希望能幫助到你!


顯然,我的建設有一個缺陷,因爲這種方式的FlowDocument不是通過鼠標的滾輪滾動。但是設置FlowDocumentScrollViewer控制的IsHitTestVisible屬性到錯誤解決此問題。

0

7年前問這個問題,現在我有同樣的問題,我找到了一個簡單的解決方案。下面的代碼在段落中添加一個與Flowdocument相同的段,然後滾動到結尾。

private void addSection(Section section) 
{ 
    section.Loaded += section_Loaded; 
    fdoc.Blocks.Add(section); 
} 

private void section_Loaded(object sender, RoutedEventArgs e)//scroll to end 
{ 
    var sec = sender as Section; 
    if (sec != null) 
    { 
     sec.BringIntoView(); 
    } 
} 
0

這可能是一個非常晚的答案,但我找到了一種方法來做到這一點。

//after your FlowDocumentScrollViewer(for example, x:Name="fdsv") loaded 
ScrollViewer sv = fdsv.Template.FindName("PART_ContentHost", fdsv) as ScrollViewer; 

sv.ScrollToBottom(); 
sv.ScrollToTop(); 
sv.ScrollToVerticalOffset(100); 
// etc. 

檢查IScrollInfoScrollViewer瞭解詳情。

我希望這可以幫助你。