ListBox  默认是UI虚拟化的。

1. 原生使用 
  1. <ListBox VirtualizingPanel.IsVirtualizing="True"
  2. VirtualizingPanel.VirtualizationMode="Recycling">
  3. </ListBox>

 为ListBox 设置一个ItemTemplate
  1. <DataTemplate x:Key="ListBoxDataTemplate">
  2. <Grid Loaded="Grid_Loaded">
  3. <Label Content="{Binding Name}"></Label>
  4. </Grid>
  5. </DataTemplate>
在 Grid_Loaded 事件中可以看到 实例化的次数
  1. int index = 0;
  2. private void Grid_Loaded(object sender, RoutedEventArgs e)
  3. {
  4. Console.WriteLine(index);
  5. index++;
  6. }
2. 当修改 ListBox   的Template属性时 ,UI虚拟化就失效了

  1. <Setter Property="Template">
  2. <Setter.Value>
  3. <ControlTemplate TargetType="{x:Type ListBox}">
  4. <ScrollViewer x:Name="ScrollViewer" CanContentScroll="false">
  5. <ItemsPresenter />
  6. </ScrollViewer>
  7. </ControlTemplate>
  8. </Setter.Value>
  9. </Setter>
这是标配写法ScrollViewer 中包裹 <ItemsPresenter />

这时ScrollViewer 控件的 CanContentScroll="false"(物理滚动) 要改为 CanContentScroll="true"(逻辑滚动)

ListBox 默认是逻辑滚动,(一项一项滚动)。改变模板时,需要重新设置
附件属性 ScrollViewer.CanContentScroll="True" 这个时候Ui虚拟化重新出现

3.修改Listbox 的ItemsPanel 模板添加一下代码也是可以ui虚拟化(除非你修改了ItemsPanel 模板,因为默认就是虚拟化的。)
  1. <Setter Property="ItemsPanel">
  2. <Setter.Value>
  3. <ItemsPanelTemplate >
  4. <VirtualizingStackPanel Orientation="Vertical"/>
  5. </ItemsPanelTemplate>
  6. </Setter.Value>
  7. </Setter>

全部代码
  1. <Window x:Class="VirtualizingStackPanelDemo.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="525">
  5. <Window.Resources>
  6. <DataTemplate x:Key="ListBoxDataTemplate">
  7. <Grid Loaded="Grid_Loaded">
  8. <Label Content="{Binding Name}"></Label>
  9. </Grid>
  10. </DataTemplate>
  11. <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
  12. <Setter Property="BorderThickness" Value="0"></Setter>
  13. <Setter Property="Margin" Value="0 0 5 0"></Setter>
  14. <Setter Property="MinWidth" Value="150"></Setter>
  15. <Setter Property="MaxHeight" Value="350"></Setter>
  16. <Setter Property="Height" Value="350"></Setter>
  17. <Setter Property="SnapsToDevicePixels" Value="True" />
  18. <Setter Property="ScrollViewer.CanContentScroll" Value="True"></Setter>
  19. <Setter Property="Template">
  20. <Setter.Value>
  21. <ControlTemplate TargetType="{x:Type ListBox}">
  22. <ScrollViewer x:Name="ScrollViewer" CanContentScroll="True">
  23. <ItemsPresenter />
  24. </ScrollViewer>
  25. </ControlTemplate>
  26. </Setter.Value>
  27. </Setter>
  28. <!--<Setter Property="ItemsPanel">
  29. <Setter.Value>
  30. <ItemsPanelTemplate >
  31. <VirtualizingStackPanel Orientation="Vertical"/>
  32. </ItemsPanelTemplate>
  33. </Setter.Value>
  34. </Setter>-->
  35. </Style>
  36. </Window.Resources>
  37. <Grid>
  38. <ListBox Style="{StaticResource ListBoxStyle}" ItemTemplate="{StaticResource ListBoxDataTemplate}" x:Name="listbox"></ListBox>
  39. </Grid>
  40. </Window>

总结 :

1.Listbox 默认虚拟化

2.修改ListBox Template时 ,重新设置ListBox 附加属性ScrollViewer.CanContentScroll 为"True",以及
<ScrollViewer CanContentScroll="True">
<ItemsPresenter />
</ScrollViewer>



WPF listbox UI虚拟化的更多相关文章

  1. 【WPF】UI虚拟化之------自定义VirtualizingWrapPanel

    原文:[WPF]UI虚拟化之------自定义VirtualizingWrapPanel 前言 前几天QA报了一个关于OOM的bug,在排查的过程中发现,ListBox控件中被塞入了过多的Item,而 ...

  2. WPF的UI虚拟化

    许多时候,我们的界面上会呈现大量的数据,如包含数千条记录的表格或包含数百张照片的相册.由于呈现UI是一件开销比较大的动作,一次性呈现数百张照片就目前的电脑性能来说是需要占用大量内存和时间的.因此需要对 ...

  3. WPF之UI虚拟化

    在WPF应用程序开发过程中,大数据量的数据展现通常都要考虑性能问题.有下面一种常见的情况:原始数据源数据量很大,但是某一时刻数据容器中的可见元素个数是有限的,剩余大多数元素都处于不可见状态,如果一次性 ...

  4. wpf 客户端【JDAgent桌面助手】开发详解(三) 瀑布流效果实现与UI虚拟化优化大数据显示

    目录区域: 业余开发的wpf 客户端终于完工了..晒晒截图 wpf 客户端[JDAgent桌面助手]开发详解-开篇 wpf 客户端[JDAgent桌面助手]详解(一)主窗口 圆形菜单... wpf 客 ...

  5. WPF ListBox/ListView/DataGrid 虚拟化时的滚动方式

    ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置.因此ListBox的默认模板中,含有ScrollVie ...

  6. windowsphone 瀑布流&ui虚拟化

    瀑布流已经有点年代了吧,不过wp上还真是挺少资料的.今天抽空把自己之前搞过的东西写出来,避免大家重复劳动. 一.简单的瀑布流排版加入ui虚拟化. 最近看了 段博琼  ui虚拟化的一篇博文,链接:htt ...

  7. Windows phone UI虚拟化和数据虚拟化(二)

    书接上回的Windows phone UI虚拟化和数据虚拟化(一)我们学习了wp的ui虚拟化.今天来和大家分享一下wp的数据虚拟化. 并同时感谢我的同事dgwutao在编写此文时给我的巨大帮助,3ks ...

  8. Windows phone UI虚拟化和数据虚拟化(一)

    今天和大家分享一些关于windows phone ui虚拟化和数据虚拟化的一些知识. 也顺便回答我上一篇[LongListSelector 控件 在 wp7 和wp8中的不同之处]里,留下的那个问题, ...

  9. WPF MVVM UI分离之《交互与数据分离》 基础才是重中之重~delegate里的Invoke和BeginInvoke 将不确定变为确定系列~目录(“机器最能证明一切”) 爱上MVC3系列~全局异常处理与异常日志 基础才是重中之重~lock和monitor的区别 将不确定变成确定~我想监视我的对象,如果是某个值,就叫另一些方法自动运行 将不确定变成确定~LINQ DBML模型可以对

    WPF MVVM UI分离之<交互与数据分离>   在我们使用WPF过程中,不可避免并且超级喜欢使用MVVM框架. 那么,使用MVVM的出发点是视觉与业务逻辑分离,即UI与数据分离 诸如下 ...

随机推荐

  1. LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告

    https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...

  2. .net_ckeditor+ckfinder的图片上传配置

    CKEditor和CKFinder的最新版可以到官方网站(http://cksource.com)上下载获得. 把以上两个资源放到网站的根目录: /CKEditor 和 /CKFinder (不区分大 ...

  3. aspcms常见问题解决方案

    1.产品详细页读取多张产品图片(栏目类型:产品){aspcms:cimages count=16 contentid=[content:id]}<li onmouseover="sho ...

  4. Xcode工程使用CocoaPods管理第三方库新建工程时出现错误

    工程使用CocoaPods管理第三方库,在新的目录update版本的时候出现如下问题   问题1描述: diff: /../Podfile.lock: No such file or director ...

  5. SAP ECC FI配置文档

    SAP ECC 6.0 Configuration Document Financial Accounting (FI) Table of Content TOC \O "1-2" ...

  6. 康力优蓝机器人 -- 优友U05类人型机器人发布

    [寒武计划]优友U05类人型机器人发布: http://digi.tech.qq.com/a/20151124/043234.htm?pgv_ref=aio2015&ptlang=2052 北 ...

  7. CSS基础(一):开篇

    背景 HTML是一种超文本标记语言,用来定义文档的结构和内容,例如标题.段落和列表等等,而文档内容如何渲染.如何展示,这就需要样式来修饰了.CSS正是可以与HTML很好地结合.如果将HTML比作水,那 ...

  8. CreateProcessAsUser,C#写的windows服务弹框提示消息或者启动子进程

    服务(Service)对于大家来说一定不会陌生,它是Windows 操作系统重要的组成部分.我们可以把服务想像成一种特殊的应用程序,它随系统的“开启-关闭”而“开始-停止”其工作内容,在这期间无需任何 ...

  9. Ubuntu下PHP的扩展

    Ubuntu版本:14.04 1. 下载php-5.5.10.tar.bz2,并解压. 2.  终端进入解压后的目录php-5.5.10,运行configure.(输入命令./configure) 3 ...

  10. ruby 中文字符to_json后乱码(unicode)

    今天遇到一个中文to_json问题 text = "第1章 青豆 不要被外表骗了" text.to_json => "\"\\u7b2c1\\u7ae0 ...