Scrollviewer logical scrolling with itemscontrol

送分小仙女□ 提交于 2020-01-15 23:25:46

问题


In a WPF app I have a ScrollViewer, in which is an ItemsControl, the items of which are databound to a collection, and I have a template specified for the items. I want the ScrollViewer to use logical scrolling, so I set the ScrollViewer.CanContentScroll="True" flag, and set the ItemsPanel template of the ItemsControl to be a StackPanel.

However, the scrolling is still physical rather than logical. What am I doing wrong?

Thanks Tom


回答1:


Sorry for the late reply... This is something that Microsoft "added" to .Net 4.5 (Pixel based scrolling).

On WPF 4, TreeView does have a logical scrolling, but ListBox and ItemsControl doesn't. So how come it works on a TreeView and not on ListBox?? This is a question that should be asked since the scrolling is being managed by the VirtualizingStackPanel.

Well, the "secret" is in an internal property in VirtualizingStackPanel called IsPixelBased.

If you set it to true then you get the logical scrolling back.

However, this has a cost. It seems that with a large Items Source (even with Virtualization and Container Recycling) the scroll is sluggish. (large items source is like 50,000 or 100,000), while with physical (item based) scrolling it is not.

I hope that this issue is solved in WPF 4.5

Here is an example of having a virtualizing pabel with Pixel Based scrolling by default:

public class VSP : VirtualizingStackPanel
{
    public VSP()
    {
        typeof(VSP).GetProperty("IsPixelBased", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this, true, null);
    }
}


来源:https://stackoverflow.com/questions/9534429/scrollviewer-logical-scrolling-with-itemscontrol

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