WPF 4.0 pixel-based scrolling in VirtualizingStackPanel

自古美人都是妖i 提交于 2020-02-01 08:20:25

问题


I have the following XAML snippet:

<ItemsControl ItemsSource="..." ItemTemplate="..." VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Standard" ScrollViewer.CanContentScroll="True">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Vertical" utils:VirtualizingStackPanelAttachedProperties.IsPixelBasedScrollingEnabled="True"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Template>
        <ControlTemplate>
            <Border BorderThickness="..." Padding="..." BorderBrush="..." Background="..." SnapsToDevicePixels="True">
               <ScrollViewer Padding="..." Focusable="False">
                   <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
               </ScrollViewer>
            </Border>
        </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>

As you can see, I have ScrollViewer.CanContentScroll="True" and utils:VirtualizingStackPanelAttachedProperties.IsPixelBasedScrollingEnabled="True" (which is an attached property that sets IsPixelBased internal property to true, as suggested here).

This works as expected in Windows XP where the scrolling is fine-grained, pixel-based.
However, this does not work as expected in Windows 7 where the scrolling is still item-based, the same as in Windows XP when NOT setting the above mentioned attached property to True.
The only way I could get pixel-based scrolling working in Windows 7 was to set CanContentScroll to False, but this is turning off virtualization.

Any idea why this happens? Is it the case that somehow in Windows XP the virtualization is not really working although it is enabled?


回答1:


Most likely that internal property is being set back to false. As is mentioned in that link in CLR 4.5 you would set the ScrollUnit to pixel. So maybe you have Clr 4.5 on that system and because that isn't being set to pixel the IsPixelBased is set back. You could alter that attached behavior to set the ScrollUnit if it exists.



来源:https://stackoverflow.com/questions/14349848/wpf-4-0-pixel-based-scrolling-in-virtualizingstackpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!