北京通州网站设计公司,南宁论坛建站模板,企业网站系统手机版,深圳网站开发工程师ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置。因此ListBox的默认模板中#xff0c;含有ScrollViewer#xff0c;ScrollViewer下存放列表内容 ScrollViewer FocusVisualStyle{x:Null}Item…ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置。因此ListBox的默认模板中含有ScrollViewerScrollViewer下存放列表内容      ScrollViewer FocusVisualStyle{x:Null}ItemsPresenter SnapsToDevicePixels{TemplateBinding UIElement.SnapsToDevicePixels}//ScrollViewer   而CanContentScrolltrue支持逻辑单元Itemfalse支持物理单元像素。源码如下      /// summary///   获取或设置一个值该值指示是否支持元素 see crefT:System.Windows.Controls.Primitives.IScrollInfo / 接口允许滚动。/// /summary/// returns///   see langwordtrue / 如果 see crefT:System.Windows.Controls.ScrollViewer / 执行滚动操作使得在逻辑单元; 方面 see langwordfalse / 如果 see crefT:System.Windows.Controls.ScrollViewer / 执行滚动操作使得在物理单元方面。///    默认值为 see langwordfalse /。/// /returnspublic bool CanContentScroll{get{return (bool) this.GetValue(ScrollViewer.CanContentScrollProperty);}set{this.SetValue(ScrollViewer.CanContentScrollProperty, value);}}  滚动 1、像素滚动物理单元 ScrollViewer.CanContentScrollfalse 通过查看源码我们可以得知CanContentScroll的默认值为false。所以列表ListBox/ListView/DataGrid默认像素滚动      /// summary///   标识 see crefP:System.Windows.Controls.ScrollViewer.CanContentScroll / 依赖属性。/// /summary/// returns///   see crefP: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.CanContentScrollTrue 按照Item高宽为滚动单位。 列表项滚动时列表只会滚动到一个完整的Item不会有一个Item只显示一半的情况。   虚拟化  通过VirtualizingPanel设置列表ListBox/ListView/DataGrid是否开启虚拟化 VirtualizingPanel其它属性有  VirtualizingPanel.ScrollUnitPixel--虚拟化滚动单位像素/单元 VirtualizingPanel.IsVirtualizingTrue --是否虚拟 VirtualizingPanel.VirtualizationModeRecycling  VirtualizingPanel.CacheLengthUnitItem --缓存单位 VirtualizingPanel.CacheLength20,20-上下缓存数量   开启虚拟化为何需要设置ScrollViewer.CanContentScrollTrue 开启虚拟化后VirtualizingPanel.ScrollUnit会替换原有的ScrollViewer.CanContentScroll滚动方式 虚拟化也有物理单元与逻辑单元之分滚动单元设置会转移到VirtualizingPanel.ScrollUnit 但是ScrollViewer.CanContentScrollFalse像素滚动并不仅仅是滚动消耗性能。当数据很多时加载列表即使开启了虚化化因计算太耗性能界面一样卡顿。 有一个解决办法设置ScrollViewer.CanContentScrollTrue后在虚拟化设置中可以设置虚拟化滚动单元VirtualizingPanel.ScrollUnitPixel此即为虚拟化时的像素滚动。  另虚拟化时的列表项滚动VirtualizingPanel.ScrollUnitItem列表项   注 VirtualizingPanel.ScrollUnit和ScrollViewer.CanContentScroll的设置滚动单元一样。 设置虚拟单位为逻辑单元时滚动时会自动滚动到一个完整的项而不是滚动到项的部分。 因此当列表可见区域Items数量或者高宽会变化时列表滚动时会闪现。   列表正确开启虚拟化方式请看我的另一博客WPF 列表开启虚拟化的方式转载于:https://www.cnblogs.com/kybs0/p/7649463.html