How can I get the DataGrid to scroll smoothly when the row is larger than the viewport?

I have a DataGrid full of notes, and it is possible that the note will be higher than the height of the DataGrid. When this happens, if you try to scroll down to read the bottom half of the note, the DataGrid will immediately go to the next row.

This not only prevents users from viewing the full note, but also makes the scroll feel unstable, because it seems to be jumping.

Is there a way to tell WPF to scroll smoothly past a long note without disabling DataGrid virtualization by default?

+6
source share
2 answers

I believe that you are looking for the attached VirtualizingPanel.ScrollUnit property that you can set in the DataGrid.

If you set its Pixel value instead of the standard Item , it should do what you want.

+20
source

If you do not want to upgrade to .NET 4.5, you can still set the IsPixelBased property based on VirtualizingStackPanel. However, this property is internal in .NET 4.0, so you will have to do this through reflection.

 public static class VirtualizingStackPanelBehaviors { public static bool GetIsPixelBasedScrolling(DependencyObject obj) { return (bool)obj.GetValue(IsPixelBasedScrollingProperty); } public static void SetIsPixelBasedScrolling(DependencyObject obj, bool value) { obj.SetValue(IsPixelBasedScrollingProperty, value); } // Using a DependencyProperty as the backing store for IsPixelBasedScrolling. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsPixelBasedScrollingProperty = DependencyProperty.RegisterAttached("IsPixelBasedScrolling", typeof(bool), typeof(VirtualizingStackPanelBehaviors), new UIPropertyMetadata(false, OnIsPixelBasedScrollingChanged)); private static void OnIsPixelBasedScrollingChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { var virtualizingStackPanel = o as VirtualizingStackPanel; if (virtualizingStackPanel == null) throw new InvalidOperationException(); var isPixelBasedPropertyInfo = typeof(VirtualizingStackPanel).GetProperty("IsPixelBased", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic); if (isPixelBasedPropertyInfo == null) throw new InvalidOperationException(); isPixelBasedPropertyInfo.SetValue(virtualizingStackPanel, (bool)(e.NewValue), null); } } 

And in your xaml:

 <DataGrid> <DataGrid.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel IsItemsHost="True" local:VirtualizingStackPanelBehaviors.IsPixelBasedScrolling="True" /> </ItemsPanelTemplate> </DataGrid.ItemsPanel> </DataGrid> 
+2
source

All Articles