![](/img/trans.png)
[英]WPF FlowDocumentScrollViewer automatically scroll to bottom of the document
[英]In a WPF, how do you scroll to the bottom of a FlowDocumentScrollViewer?
普通的ScrollViewer似乎具有完成此任務的方法。 FlowDocumentScrollViewer沒有。 因此,如何解決將FlowDocumentScrollViewer設置在底部的問題。
您可以從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;
}
或者,您可以在此處使用提供的答案,並搜索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;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.