WPF ScrollViewer滚动到FlowDocument的元素



我正在开发一个聊天应用程序。有一个scrollviewer,它包含一个richtextbox,其中包含一个flowdocument,其中在其块中包含段落。现在,当添加新消息时,scrollviewer将正确地向下滚动其内容(当滚动条位于底部时自动滚动)。当向下滚动到底部时,流程文档总是最多有100个段落。(当新消息到达时,顶部的旧段落将被删除。)

我想添加的是,当我向上滚动滚动查看器到它的顶部,然后我想加载旧的消息(一种Facebook风格)。当我滚动到顶部旧消息被正确加载,但我想要实现的是,加载旧消息之前的最上面的段落仍然会显示在顶部。为此,我认为我需要计算该段落的新位置,并将滚动查看器的垂直偏移量设置为该段落的Y坐标。(在新消息加载后,scrollviewer仍然保持滚动到顶部。)

但是我怎么能检测到插入旧消息后的段落在哪里?

不幸的是,BringIntoView()不能用于此。我认为原因可能是该段在FlowDocument中,而不是直接在ScrollViewer中。我不知道你是否能看出来(或者它是这样工作的…)将ScrollViewer滚动到该段落位于顶部的位置。

另一个链接的解决方案是不好的,因为一个段落对象不能转换为FrameworkElement,只有它有一个TranslatePoint方法。

我设法做的是:因为我知道有多少行插入到FlowDocument中,我将插入的段落的大小求和,这样我就可以得到我需要滚动ScrollViewer的段落的点。然后我打电话给sw。使用那个参数ScrollToVerticalOffset,它会很好地滚动到那里。:)

这里是完整的代码,我希望它可以帮助一些人。这个函数订阅了我的ScrollViewer的ScrollChanged事件

但是请注意,这个解决方案只会正确地工作在一行中显示的段落!

    bool StopLoading = false; // This is required to ensure that only the specified amount of messages will be loaded at once.
    // If we scroll upper the messages, then don't scroll. If we scroll to bottom, scroll the messages
    private void MessageScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        var obj = sender as ScrollViewer;
        // The scrollbar is at the bottom
        if (obj.VerticalOffset == obj.ScrollableHeight)
        {
            // The while loop removes loaded messages when we have scrolled to the bottom
            Channel ch = (Channel)((ScrollViewer)sender).Tag;
            while (ch.TheFlowDocument.Blocks.Count > GlobalManager.MaxMessagesDisplayed)
            {
                ch.TheFlowDocument.Blocks.Remove(ch.TheFlowDocument.Blocks.FirstBlock);
                if (ch.MessagesLoadedFrom + 1 + GlobalManager.MaxMessagesDisplayed < GlobalManager.MaxMessagesInMemory)
                    ch.MessagesLoadedFrom++;
            }
            // The automatic scroll when the scrollbar is at the bottom
            obj.ScrollToEnd();
        }
        // The scrollbar is at the top
        else if (obj.VerticalOffset == 0) // Load older messages
        {
            if (!StopLoading)
            {
                Channel ch = (Channel)((ScrollViewer)sender).Tag;
                if (ch.MessagesLoadedFrom != 0)
                {
                    int loaded = LoadMessages(ch, GlobalManager.NumOfOldMessagesToBeLoaded);
                    double plus = first.Padding.Top + first.Padding.Bottom + first.Margin.Bottom + first.Margin.Top; // Since all of my paragraphs have the same Margin and Padding I can do this
                    double sum = 0;
                    Block temp = ch.TheFlowDocument.Blocks.FirstBlock;
                    for (int i = 0; i < loaded; i++)
                    {
                        sum += temp.FontSize + plus;
                        temp = temp.NextBlock;
                        if (temp == null)
                            break;
                    }
                    obj.ScrollToVerticalOffset(sum);
                }
                StopLoading = true;
            }
        }
        // The scrollbar is not at the top and not at the bottom. We can enable loading older messages
        else
        {
            StopLoading = false;
        }
        e.Handled = true;
    }

最新更新