普通的ScrollViewer似乎有一个方法可以实现这一点。 FlowDocumentScrollViewer没有。那么如何将FlowDocumentScrollViewer设置到底部。
答案 0 :(得分: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;
}
答案 1 :(得分:2)
或者您可以使用提供的答案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;
}
}
}
}