从 FlowDocumentScrollViewer 到 ListView 的气泡滚动事件

bubble scroll event from FlowDocumentScrollViewer to ListView

我有以下(部分)XAML:

    <ListView x:Name="logView" Grid.Row="2" ItemsSource="{Binding Logs}"
                               ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListView.ItemTemplate>
        <DataTemplate>
            <FlowDocumentScrollViewer ScrollViewer.VerticalScrollBarVisibility="Disabled"
                                      ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <FlowDocument FontSize="12" FontFamily="Calibri" PagePadding="0" TextAlignment="Left">
                    <Paragraph TextIndent="-10" Margin="10,0,0,0">
                        <Run Text="{Binding .}" />
                    </Paragraph>
                </FlowDocument>
            </FlowDocumentScrollViewer>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

LogsListView 绑定到的 IEnumerable<string>(通过 ViewModel,但这在这里无关紧要)。

如果我删除整个 <ListView.ItemTemplate>...</ListView.ItemTemplate>,我就有了我想要的鼠标滚轮滚动行为。但是对于 FlowDocumentScrollViewer 及其内容,滚动不再那么流畅了。它仍然滚动,但只是时不时地,大多数时候它会卡住。

为了解决这个问题,我遵循了 this 解决方案并在代码隐藏中创建了一个 PreviewMouseWheel 处理程序

private void BubbleScrollingToLogView(object sender, MouseWheelEventArgs e)
{
    if (!e.Handled)
    {
        e.Handled = true;
        var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
        eventArg.RoutedEvent = MouseWheelEvent;
        eventArg.Source = sender;
        logView.RaiseEvent(eventArg);
    }
}

并将其添加到 XAML:

....
<FlowDocumentScrollViewer ScrollViewer.VerticalScrollBarVisibility="Disabled"
                          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                          PreviewMouseWheel="BubbleScrollingToLogView">
....

但它并没有对行为做出任何改变。我什至尝试将 PreviewMouseWheel="BubbleScrollingToLogView" 添加到 <FlowDocument><Paragraph>,假设它们也可能捕获事件。但没有任何帮助。

那么我需要做什么才能获得 ListView 的平滑默认滚动行为?

如果您决定只使用 FlowDocumentScrollViewer,您可以将 Document 属性 绑定到视图模型上的 属性。

<FlowDocumentScrollViewer Document="{Binding Document}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
</FlowDocumentScrollViewer>

在您的视图模型中定义 FlowDocument 类型的 Document 属性。

private FlowDocument document;
public FlowDocument Document
{
    get { return document; }
    set
    {
        document = value;
        OnPropertyChanged();
    }
}

Logs 创建 FlowDocument:

var doc = new FlowDocument();
doc.FontSize = 12;
doc.FontFamily = new FontFamily("Calibri");
doc.PagePadding = new Thickness(0);
doc.TextAlignment = TextAlignment.Left;
foreach (var log in Logs)
{
    var paragraph = new Paragraph(new Run(log));
    paragraph.TextIndent = -10;
    paragraph.Margin = new Thickness(10, 0, 0, 0);
    doc.Blocks.Add(paragraph);
}
Document = doc;

禁用 FlowDocumentScrollViewer 以获得默认滚动行为就足够了。由于禁用,我没有注意到外观有任何差异。

<FlowDocumentScrollViewer ScrollViewer.VerticalScrollBarVisibility="Disabled"
                          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                          IsEnabled="False">