2017-02-11 62 views

回答

2

您可以從flowdocumentscrollviewer訪問scrollviewer。下面是例子

private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
    { 
     var scrollViewer = FindScrollViewer(flowDocumentScrollViewer); 
     scrollViewer.ScrollToBottom(); 
    } 

    public static ScrollViewer FindScrollViewer(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

嘛,肯定有效。這是否是標準做法,或者您通常不想滾動到flowdocumentscrollviewer的底部? – PPFY

+0

這是一種沒有暴露的功能的實用方法。我沒有看到使用它的任何問題。 –

1

或者您可以使用所提供的答案here,並搜索的ScrollViewer:

public static void ScrollToEnd(FlowDocumentScrollViewer fdsv) 
{ 
    ScrollViewer sc = FindVisualChildren<ScrollViewer>(fdsv).First() as ScrollViewer; 
    if (sc != null) 
     sc.ScrollToEnd(); 
} 


public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject 
{ 
    if (depObj != null) 
    { 
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) 
     { 
      DependencyObject child = VisualTreeHelper.GetChild(depObj, i); 
      if (child != null && child is T) 
      { 
       yield return (T)child; 
      } 

      foreach (T childOfChild in FindVisualChildren<T>(child)) 
      { 
       yield return childOfChild; 
      } 
     } 
    } 
}