首页 技术 正文
技术 2022年11月16日
0 收藏 323 点赞 2,336 浏览 2782 个字

ListBox的滚动方式 分为像素滚动和列表项滚动

通过ListBox的附加属性ScrollViewer.CanContentScroll来设置。因此ListBox的默认模板中,含有ScrollViewer,ScrollViewer下存放列表内容

    <ScrollViewer FocusVisualStyle="{x:Null}">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
</ScrollViewer>

而CanContentScroll,true支持逻辑单元(Item),false支持物理单元(像素)。源码如下:

    /// <summary>
/// 获取或设置一个值,该值指示是否支持元素 <see cref="T:System.Windows.Controls.Primitives.IScrollInfo" /> 接口允许滚动。
/// </summary>
/// <returns>
/// <see langword="true" /> 如果 <see cref="T:System.Windows.Controls.ScrollViewer" /> 执行滚动操作使得在逻辑单元; 方面 <see langword="false" /> 如果 <see cref="T:System.Windows.Controls.ScrollViewer" /> 执行滚动操作使得在物理单元方面。
/// 默认值为 <see langword="false" />。
/// </returns>
public bool CanContentScroll
{
get
{
return (bool) this.GetValue(ScrollViewer.CanContentScrollProperty);
}
set
{
this.SetValue(ScrollViewer.CanContentScrollProperty, value);
}
}

滚动

1、像素滚动(物理单元) ScrollViewer.CanContentScroll=false

通过查看源码,我们可以得知CanContentScroll的默认值为false。所以列表ListBox/ListView/DataGrid默认像素滚动

    /// <summary>
/// 标识 <see cref="P:System.Windows.Controls.ScrollViewer.CanContentScroll" /> 依赖属性。
/// </summary>
/// <returns>
/// <see cref="P:System.Windows.Controls.ScrollViewer.CanContentScroll" /> 依赖项属性的标识符。
/// </returns>
[CommonDependencyProperty]
public static readonly DependencyProperty CanContentScrollProperty = DependencyProperty.RegisterAttached(nameof (CanContentScroll), typeof (bool), typeof (ScrollViewer), (PropertyMetadata) new FrameworkPropertyMetadata(BooleanBoxes.FalseBox));
  [FriendAccessAllowed]
internal static class BooleanBoxes
{
internal static object TrueBox = (object) true;
internal static object FalseBox = (object) false; internal static object Box(bool value)
{
if (value)
return BooleanBoxes.TrueBox;
return BooleanBoxes.FalseBox;
}
}

像素滚动的优点:平滑–因为按照像素滚动,肉眼分辨较低。

像素滚动的缺点:耗性能-列表中每个项,都要计算出宽高具体数值,且滚动时时计算。如果列表中数量过多,就相当卡了。

2、列表项滚动(逻辑单元) ScrollViewer.CanContentScroll=”True”

按照Item高宽为滚动单位。

列表项滚动时,列表只会滚动到一个完整的Item,不会有一个Item只显示一半的情况。

虚拟化 

通过VirtualizingPanel,设置列表ListBox/ListView/DataGrid是否开启虚拟化

VirtualizingPanel其它属性有:

VirtualizingPanel.ScrollUnit=”Pixel”–虚拟化滚动单位(像素/单元)

VirtualizingPanel.IsVirtualizing=”True” –是否虚拟

VirtualizingPanel.VirtualizationMode=”Recycling”

VirtualizingPanel.CacheLengthUnit=”Item” –缓存单位

VirtualizingPanel.CacheLength=”20,20″-上下缓存数量

开启虚拟化:为何需要设置ScrollViewer.CanContentScroll=”True”?

开启虚拟化后,VirtualizingPanel.ScrollUnit会替换原有的ScrollViewer.CanContentScroll滚动方式

虚拟化也有物理单元与逻辑单元之分,滚动单元设置会转移到VirtualizingPanel.ScrollUnit

但是ScrollViewer.CanContentScroll=”False”像素滚动,并不仅仅是滚动消耗性能。当数据很多时加载列表,即使开启了虚化化,因计算太耗性能,界面一样卡顿。

有一个解决办法,设置ScrollViewer.CanContentScroll=”True”后,在虚拟化设置中,可以设置虚拟化滚动单元VirtualizingPanel.ScrollUnit=”Pixel”,此即为虚拟化时的像素滚动。

另:虚拟化时的列表项滚动,VirtualizingPanel.ScrollUnit=”Item”列表项

注:

VirtualizingPanel.ScrollUnit和ScrollViewer.CanContentScroll的设置滚动单元一样。

设置虚拟单位为逻辑单元时,滚动时会自动滚动到一个完整的项,而不是滚动到项的部分。

因此当列表可见区域,Items数量或者高宽会变化时,列表滚动时会闪现。

列表正确开启虚拟化方式,请看我的另一博客:WPF 列表开启虚拟化的方式

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,038
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,523
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,371
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,152
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,785
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,867