Smooth Pixel Scrolling in WPF ListView / ListBox

21 篇文章 0 订阅

WPF comes with lots of fun and advantages. We can change the look and behavior of a control very easily which would have been very cumbersome when we are in normal winforms applications. WPF has few flexibilities like Superior Binding capabilities, Animation, Transformation, Virtualizations etc. Each of these made the framework richer in all respect.

In WPF, ListBox and ListView scrolls based on the items. The Scrolling on those controls are not smooth as the ScrollBar doesnt animate the scroller as we do with other controls. Sometimes you will also see the scroller is produced with incorrect size. I mean to say, the same scroller when it is in the top, the size is different than when its in the bottom. This happens basically because of Virtualization implemented on those controls.

ListBox and ListView actually has VirtualizingStackPanel enabled by default. This means, WPF will not actually create all the items and hence determine the scrollbar, rather the size of the ScrollBar is calculated based on the average multiple of items in its display with the number of items actually there in the CollectionView. Thus if you have items which are in display bigger than which are not in view, you will see the size of the ScrollBar increased abruptly whenever you scroll down a bit.


You can see in the above snap, the irregular sized items will make the scrollbar to behave strange.

The easiest fix to this issue is you set
ScrollViewer.CanContentScroll = false

This will make the scrollviewer to disable the scrolling and hence the ListBox scroller appear.
The main problem with this approach is that, if you Disable the ContentScroll property of the ScrollViewer, it means it will completely stop Virtualizing. And hence if you have a big dataItems, it will cause huge performance issue.

You can also create your own scroolViewer using IScrollInfo. You have to create your custom panel to implement your scroller. This will be best.
I am also thinking to build one panel which might use Virtualizing as well. I will give you the idea when I finish.

For time being you can use CanContentScroll = false.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值