惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
腾讯CDC
A
About on SuperTechFans
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
D
DataBreaches.Net
D
Docker
宝玉的分享
宝玉的分享
量子位
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
Last Week in AI
Last Week in AI
H
Help Net Security
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence

博客园 - 阿冠

git报错unable to read tree xxxxxx socket tcp 报错误码 103/10053 原因 NuGet 未能为 SSL/TLS 安全通道建立信任关系 NuGet 未能为 SSL/TLS 安全通道建立信任关系 Visual Studio Font Cache 字体缓存路径下的文件太大了 ControlTemplate 中 Bingding 附加属性时需要加入 Path WPF使用第三方的字体(TTF文件) Base class for cloning an object in C# 关闭Windows 系统当前连接的Wifi以及判断物理\虚拟网卡,有线\无线网卡 - 阿冠 - 博客园 WPF 竖排文字 WPF 将DLL嵌入EXE文件(安装包) WPF 程序自删除(自毁)|卸载程序删除 记一次纠结Macbook 重装OS X的系统 指定的元素已经是另一个元素的逻辑子元素。请先将其断开连接。(解决问题总结) 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转换为类型“System.Collections.Generic.IList`1 foreach---集合已修改;可能无法执行枚举操作。 WPF_View中控件使用单例ViewModel 判断s2是否能够被通过s1做循环移位(rotate)得到的字符串是否包含 多列转1列 SqlServer 实现oracle10g的 wmsys.wm_concat()--for xml path('')
WPF listbox UI虚拟化
阿冠 · 2016-04-27 · via 博客园 - 阿冠

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>