Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中,如何滚动到FlowDocumentScrollViewer的底部?_C#_Wpf_Visual Studio_Xaml - Fatal编程技术网

C# 在WPF中,如何滚动到FlowDocumentScrollViewer的底部?

C# 在WPF中,如何滚动到FlowDocumentScrollViewer的底部?,c#,wpf,visual-studio,xaml,C#,Wpf,Visual Studio,Xaml,普通的ScrollViewer似乎有一种方法可以实现这一点。FlowDocumentScrollViewer不支持。那么我如何将FlowDocumentScrollViewer设置到底部。您可以从FlowDocumentScrollViewer访问scrollviewer。下面是一个例子 private void MainWindow_Loaded(object sender, RoutedEventArgs e) { var scrollViewer = FindSc

普通的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;
            }
        }
    }
}
公共静态无效滚动结束(FlowDocumentScrollViewer fdsv)
{
ScrollViewer sc=FindVisualChildren(fdsv).First()作为ScrollViewer;
如果(sc!=null)
sc.ScrollToEnd();
}
公共静态IEnumerable FindVisualChildren(DependencyObject depObj),其中T:DependencyObject
{
if(depObj!=null)
{
for(int i=0;i
好吧,那当然行。这样做是标准做法,还是您通常不想滚动到flowdocumentscrollviewer的底部?这是一种未公开的函数实用方法。我觉得用这个没什么问题。