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. How to set colors of HTML tables

    There is a simple way to set the color of tables: use the bgcolor attribute of tag <table>.   ...

  2. Android Screen Orientation Change (Screen Rotation) Example

    原文见: http://techblogon.com/android-screen-orientation-change-rotation-example/#

  3. Xcode 6 UITextField 键盘不弹出

    iOS Simulator -> Hardware -> Keyboard Uncheck "Connect Hardware Keyboard"

  4. jeos没有消亡,但看 debian 的 netinst .iso格式,那就是jeos的系统!

    曾经ubuntu推出专供轻量硬件(如虚拟机)方式的just os格式的.iso [小巧.轻量.快速.干净] 但在 ubuntu 8.04后 再也没有继续 ...... 可惜 不曾想,ubuntu的老爸 ...

  5. 老鼠跑猫叫主人惊醒c++观察者模式实现

    这个题目算是比较经典的观察者模式了,老鼠作为一个Subject,主动发出跑的动作,紧跟着猫由于老鼠的跑而发出叫声,主人也被惊醒,在这里猫跟主人都是被动的,是观察者角色,代码实现如下: class CS ...

  6. VS2005 / windows sdk7.1配置

    VS2005工程需要调用一些后期VS带的库 1. VS2005 安装顺序 1.vs20052.msdn(optional)3.VS80sp1-KB926601-X86-ENU_SP1.exe4.VS8 ...

  7. AX ERP 真正的自动批处理

    AX real batch job- AX ERP 真正的批处理 在AX3标准功能中,自动化任务是利用Batch来进行自动化处理任务,标准功能的局限是无法真正做到无人值守.比如服务器重启,必须手动去开 ...

  8. EventBus使用介绍

    EventBus是Android下高效的发布/订阅事件总线机制.作用是可以代替传统的Intent,Handler,Broadcast或接口函数在Fragment,Activity,Service,线程 ...

  9. Linq专题之var关键字

    在c#1.0,c#2.0中声明一个变量时,总是要指定变量的类型,如果不指定变量类型编译器就会报错,产生编译错误.在c#3.0中我们可以不指定变量的具体类型,而使用一个新的关键字"var&qu ...

  10. H5+CSS3实现手指滑动切换图片

    包含3个文件:html.slider-H5.js.jquery.js(自行下载).在html中可配置滑动参数.具体代码如下: HTML代码: <!DOCTYPE HTML> <htm ...