1
一個正常的ScrollViewer似乎有一個方法來完成這個。 FlowDocumentScrollViewer沒有。那麼我該如何去設置一個FlowDocumentScrollViewer到底部。在WPF中,你如何滾動到FlowDocumentScrollViewer的底部?
一個正常的ScrollViewer似乎有一個方法來完成這個。 FlowDocumentScrollViewer沒有。那麼我該如何去設置一個FlowDocumentScrollViewer到底部。在WPF中,你如何滾動到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;
}
或者您可以使用所提供的答案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;
}
}
}
}
嘛,肯定有效。這是否是標準做法,或者您通常不想滾動到flowdocumentscrollviewer的底部? – PPFY
這是一種沒有暴露的功能的實用方法。我沒有看到使用它的任何問題。 –