gpt4 book ai didi

wpf - 检测FlowDocument更改并滚动

转载 作者:行者123 更新时间:2023-12-04 17:05:40 25 4
gpt4 key购买 nike

我想检测(最好通过事件)何时在FlowDocument中添加,更改等内容,以及何时进行检测,以使显示FlowDocumentScrollViewerFlowDocument自动滚动到末尾。

最佳答案

您可以通过创建文本范围并监视其变化来检测FlowDocument中的变化。滚动到底部更加困难,因为您必须找到ScrollViewer。另外,为了提高性能,您不希望在每次更改时都重做所有滚动计算,因此应使用DispatcherOperations

综上所述,这段代码应该可以解决问题:

var range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
object operation = null;

range.Changed += (obj, e) =>
{
if(operation==null)
operation = Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
{
operation = null;

var scrollViewer = FindFirstVisualDescendantOfType<ScrollViewer>(flowDocument);
scrollViewer.ScrollToBottom();
});
};

其中 FindFirstVisualDescendantOfType是使用 VisualTreeHelper.GetChildrenCount()VisualTreeHelper.GetChild()对视觉树进行的简单的深度优先前缀搜索,并返回找到的第一个指定类型的Visual。

请注意,出于一般性考虑,我不会在代码顶部预先计算scrollViewer,因为 FlowDocumentScrollViewer的模板可以更改。如果这不会发生,可以通过在 .ApplyTemplate()上调用 FlowDocumentScrollViewer,然后在注册事件处理程序之前计算 scrollViewer来加快此代码的速度:
var range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
object operation = null;

flowDocument.ApplyTemplate();
var scrollViewer = FindFirstVisualDescendantOfType<ScrollViewer>(flowDocument);

range.Changed += (obj, e) =>
{
if(operation==null)
operation = Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
{
operation = null;
scrollViewer.ScrollToBottom();
});
};

请注意,由于 scrollViewer.GetTemplateChild("PART_ContentHost") protected ,我们不能简单地调用 GetTemplateChild并跳过可视树搜索。

关于wpf - 检测FlowDocument更改并滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1655208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com