I have the following (part of) 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>
Logs
is an IEnumerable<string>
that the ListView
is bound to (via a ViewModel, but that shouldn't matter here).
If I remove the whole <ListView.ItemTemplate>...</ListView.ItemTemplate>
, I have the mouse wheel scrolling behaviour I want. But with the FlowDocumentScrollViewer
and its content, scrolling does not work as smoothly any more. It still scrolls, but just every now and then, most of the time it gets stuck.
Trying to solve this, I followed this solution and created a PreviewMouseWheel handler in the codebehind
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);
}
}
and added it in the XAML:
....
<FlowDocumentScrollViewer ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
PreviewMouseWheel="BubbleScrollingToLogView">
....
But it did not make any change in behaviour. I even tried adding PreviewMouseWheel="BubbleScrollingToLogView"
to <FlowDocument>
and <Paragraph>
, assuming that those might catch the event as well. But nothing helped.
So what do I need to do to get the smooth, default scrolling behaviour of the ListView
?