WrapPanel 实现虚拟化

 WrapPanel 实现虚拟化

控件名:VirtualizingWrapPanel

作者:WPFDevelopersOrg

原文链接:    https://github.com/WPFDevelopersOrg/WPFDevelopers

  • 框架使用大于等于.NET40

  • Visual Studio 2022;

  • 项目使用 MIT 开源许可协议;

  • 众所周知 WPFStackPanel 在加载大量数据时性能会特别差,但是官方提供了一个虚拟化容器VirtualizingStackPanel[1]

    • VirtualizingStackPanel.IsVirtualizing 附加属性设置为 true时就开启虚拟化。

    • VirtualizingStackPanel.IsVirtualizing 附加属性设置为 falseVirtualizingStackPanel行为与普通StackPanel属性的行为相同。

  • WrapPanel 默认是不支持虚拟化的,所以需要自行实现。

1) VirtualizingWrapPanel 查看源码1[2]  |   VirtualizingWrapPanel 查看源码2[3]

2) 准备数据HospitalList.cs如下:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Media;namespace WPFDevelopers.Minimal.Sample.Models
{public class HospitalList : ObservableCollection<Hospital>{public HospitalList(){var hospitals = new string[] { "No. 189, Grove St, Los Angeles", "No. 3669, Grove St, Los Angeles" };var names = new string[] { "Doctor Fang", "Judge Qu" };var images = new string[] { "https://pic2.zhimg.com/80/v2-0711e97955adc9be9fbcff67e1007535_720w.jpg",//"https://pic2.zhimg.com/80/v2-5b7f84c63075ba9771f6e6dc29a54615_720w.jpg","https://pic3.zhimg.com/80/v2-a3d6d8832090520e7ed6c748a8698e4e_720w.jpg","https://pic3.zhimg.com/80/v2-de7554ac9667a59255fe002bb8753ab6_720w.jpg"};var state = 0;for (var i = 1; i < 10000; i++){Add(new Hospital { Id = $"9999{i}", DoctorName = i % 2 == 0 ? names[0]:names[1], HospitalName = i % 2 == 0 ? hospitals[0] : hospitals[1] ,State = state ,UserImage = images[state] });state++;if (state > 2)state = 0;}}}public class Hospital{public string Id { get; set; }public string DoctorName { get; set; }public string HospitalName { get; set; }public string UserImage { get; set; }public int State { get; set; }}
}

3) 新建展示VirtualizingWrapPanelExample.xaml如下:

<ws:Window x:Class="WPFDevelopers.Minimal.Sample.ExampleViews.VirtualizingWrapPanelExample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:ws="https://github.com/WPFDevelopersOrg/WPFDevelopers.Minimal"xmlns:local="clr-namespace:WPFDevelopers.Minimal.Sample.ExampleViews"xmlns:model="clr-namespace:WPFDevelopers.Minimal.Sample.Models"xmlns:converts="clr-namespace:WPFDevelopers.Minimal.Sample.Converts"mc:Ignorable="d" WindowStartupLocation="CenterScreen"Title="System V1.0" Height="450" Width="900"><Window.Resources><model:HospitalList x:Key="myHospitalList"/><converts:StateConvert  x:Key="stateConvert"></converts:StateConvert></Window.Resources><Grid Margin="4"><WrapPanel HorizontalAlignment="Left"><WrapPanel.Resources><Style TargetType="Border"><Setter Property="Padding" Value="2"></Setter><Setter Property="BorderThickness" Value="1"></Setter></Style><Style TargetType="Rectangle"><Setter Property="Width" Value="15"></Setter><Setter Property="Height" Value="15"></Setter><Setter Property="Opacity" Value=".2"></Setter></Style></WrapPanel.Resources><WrapPanel><Border BorderBrush="Green"><Rectangle Fill="Green"/></Border><TextBlock Text="Idle" Foreground="Black" Margin="4,0"/></WrapPanel><WrapPanel><Border BorderBrush="Orange"><Rectangle Fill="Orange"/></Border><TextBlock Text="Slightly Idle" Foreground="Black" Margin="4,0"/></WrapPanel><WrapPanel><Border BorderBrush="Red"><Rectangle Fill="Red"/></Border><TextBlock Text="Busy" Foreground="Black" Margin="4,0"/></WrapPanel></WrapPanel><TextBlock HorizontalAlignment="Right" Foreground="Black"Margin="4,2" FontSize="16"><Run Text="Count:"></Run><Run Text="{Binding ElementName=DocumentsList,Path=.Items.Count,Mode=OneTime}"></Run></TextBlock><ListBox x:Name="DocumentsList"ItemsSource="{Binding Source={StaticResource myHospitalList}}"Margin="0,24,0,0"><ListBox.ItemTemplate><DataTemplate><Border BorderBrush="{Binding State,Converter={StaticResource stateConvert}}" BorderThickness="1"Width="196"Height="94"><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Rectangle Fill="{Binding State,Converter={StaticResource stateConvert}}" Opacity=".2" Grid.ColumnSpan="2" Grid.RowSpan="3"/><Border Grid.RowSpan="2" Grid.Column="0" Width="60" Height="60"Margin="0,4,0,0" CornerRadius="10"><Border.Background><ImageBrush ImageSource="{Binding UserImage}" Stretch="Uniform"/></Border.Background></Border><TextBlock Grid.Column="1" Grid.Row="0"Text="{Binding Path=Id}" Margin="0,4,0,0"/><TextBlock Grid.Column="1" Grid.Row="1"Text="{Binding Path=DoctorName}"/><TextBlock Grid.ColumnSpan="2" Grid.Row="2"Padding="10,0"Text="{Binding Path=HospitalName}" TextTrimming="CharacterEllipsis"/></Grid></Border></DataTemplate></ListBox.ItemTemplate><ListBox.Template><ControlTemplate><Border CornerRadius="2" BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"><ScrollViewer x:Name="ScrollViewer"Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="0"  IsTabStop="False"><ItemsPresenter /></ScrollViewer></Border></ControlTemplate></ListBox.Template><ListBox.ItemsPanel><ItemsPanelTemplate><ws:VirtualizingWrapPanel ItemWidth="200"ItemHeight="100"/></ItemsPanelTemplate></ListBox.ItemsPanel></ListBox></Grid>
</ws:Window>

4) 状态StateConvert.cs如下:

using System;
using System.Windows.Data;
using System.Windows.Media;namespace WPFDevelopers.Minimal.Sample.Converts
{public class StateConvert : IValueConverter{public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo cultureInfo){var color = Brushes.Green;if (value != null){var state = int.Parse(value.ToString());switch (state){case 0:color = Brushes.Green;break;case 1:color = Brushes.Orange;break;case 2:color = Brushes.Red;break;}}return color;}public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo cultureInfo){throw new NotImplementedException();}}
}
c34387d350a2e01ded2831af0ed23f81.gif

参考资料

[1]

VirtualizingStackPanel: https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.controls.virtualizingstackpanel?view=windowsdesktop-6.0

[2]

VirtualizingWrapPanel 查看源码1: https://github.com/samueldjack/VirtualCollection/blob/master/VirtualCollection/VirtualCollection/VirtualizingWrapPanel.cs

[3]

VirtualizingWrapPanel 查看源码2: https://github.com/WPFDevelopersOrg/WPFDevelopers.Minimal/blob/main/src/WPFDevelopers.Minimal/WPFDevelopers.Minimal.Shared/Controls/VirtualizingWrapPanel/VirtualizingWrapPanel.cs

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/282675.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pdo连接mysql数据库(简洁明了)

一 实例化pdo对象 $dsn "mysql:dbnametest;host127.0.0.1"; $pdo new PDO($dsn,root,root);二 数据查询 1、如果不根据用户传过来的值进行操作,可以直接query sql $dsn "mysql:dbnametest;host127.0.0.1"; $pdo new PDO($dsn,root,root); $sql &qu…

一次微信小程序的快速开发体验

起因 事情是这样的 一天早上组里还早激烈的讨论某个项目的可用性和发展前景&#xff0c;突然老大说了句&#xff0c;能不能做个小程序的版本呢&#xff1f;然后大家纷纷讨论起来&#xff0c;有反对有支持&#xff0c;我就说了一句&#xff0c;刚出来的时候搞过一会。。。然后就…

造数据时踏过的坑

1.在产生随机数时,在数据规模很大的时候很难出现自己要的模型,比如某个条件的数据量,此时要写一个方法,来造一批这样的数据 2.将控制数量,文件路径写成配置文件的形式,以免重复打包 3.输入输出文件夹,可以配置以免重复打包 转载于:https://www.cnblogs.com/rocky-AGE-24/p/7376…

如何证明 ConcurrentDictionary 字典操作不全是线程安全的

前言最近&#xff0c;看到一篇文章&#xff0c;讲到《ConcurrentDictionary字典操作竟然不全是线程安全的&#xff1f;》。首先&#xff0c;这个结论是正确的&#xff0c;但文中给出的一个证明例子&#xff0c;我觉得是有问题的。相关代码如下&#xff1a;using System.Collect…

微型计算机及接口技术试题,1月自考微型计算机及其接口技术试题及答案解析...

《1月自考微型计算机及其接口技术试题及答案解析》由会员分享&#xff0c;可在线阅读&#xff0c;更多相关《1月自考微型计算机及其接口技术试题及答案解析(11页珍藏版)》请在人人文库网上搜索。1、精品自学考试资料推荐全国 2018年 1月自考微型计算机及其接口技术试题课程代码…

16-djongo中间件学习

目录 前戏 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录&#xff0c;把没有登录的用户请求跳转到登录页面。我们通过给几个特定视图函数加装饰器实现了这个需求。但是以后添加的视图函数可能也需要加上装饰器&#xff0c;这样是不是稍微有点繁琐。 学完…

PHP基础(必须熟练掌握的基础)

<?php/*** 三元运算符的应用*/ /* $a 10; $b 15; echo $a > $b ? 1 : 0; */ // 注:php7新添加的运算符比较运算符x<>y // 如果x和y相等,就返回0,如果x>y,就返回1,如果x的值小于y,就返回-1/* $a "aaa"; $b "bbb"; echo $a.$b; *//*** …

子进程无法从标准输入读取数据

每个process对象最多只能调用一次start()方法&#xff0c;join([timeout])方法会阻塞调用process对象的进程&#xff0c;直到timeout时间超时&#xff0c;或者process进程退出。如果timeout设置为None&#xff0c;则无超时时间。对于linux操作系统的进程管理&#xff0c;父进程…

Eclipse控制项目的访问名称

Eclipse控制web项目的访问名称 web项目的访问路径&#xff08;名称&#xff09;修改 1.点击项目右键-》properties找到Context root 修改成我们需要的名字即可转载于:https://www.cnblogs.com/pypua/articles/7379950.html

计算机一级选择题已做完确认,计算机一级选择题(附答案)

点击蓝字关注我们(1)按照需求功能的不同&#xff0c;信息系统已形成各种层次&#xff0c;计算机应用于管理是开始于:()A)信息处理B)人事管理C)决策支持D)事务处理正确答案&#xff1a;A解析&#xff1a;计算机用于管理&#xff0c;起源于计算机在办公应用中对大量信息、数据的处…

参加51CTO培训,PMP考试通过啦

为什么选择考PMP&#xff1f;先介绍下自己的情况&#xff0c;毕业三年&#xff0c;单位类似于平台&#xff0c;不做技术&#xff0c;常态的工作是文案、商务、市场都会涉及些&#xff0c;对未来也有些迷茫。受前辈点拨可以学一些通用的技能&#xff0c;于是我选择了PMP&#xf…

如何查看服务器并发请求连接数

https://wenku.baidu.com/view/fb553d795acfa1c7aa00cc27?pcf2#1 转载于:https://www.cnblogs.com/linewman/p/9918760.html

C# 二十年语法变迁之 C# 5 和 C# 6参考

C# 二十年语法变迁之 C# 5 和 C# 6参考https://benbowen.blog/post/two_decades_of_csharp_ii/自从 C# 于 2000 年推出以来&#xff0c;该语言的规模已经大大增加&#xff0c;我不确定任何人是否有可能在任何时候都对每一种语言特性都有深入的了解。因此&#xff0c;我想写一系…

非涉密计算机检查的通知,关于开展非涉密计算机及可移动存储介质专项清理活动的紧急通知...

关于在全校范围内开展非涉密计算机及可移动存储介质专项清理活动的紧急通知密办字[2009]01号各单位&#xff1a;为有效遏制木马病毒和恶意代码的蔓延趋势&#xff0c;现在校内开展一次非涉密计算机及可移动存储介质的专项清理活动&#xff0c;要求如下&#xff1a;1、所有涉密人…

Spring Cloud构建微服务架构:服务消费(基础)

使用LoadBalancerClient在Spring Cloud Commons中提供了大量的与服务治理相关的抽象接口&#xff0c;包括DiscoveryClient、这里我们即将介绍的LoadBalancerClient等。对于这些接口的定义我们在上一篇介绍服务注册与发现时已经说过&#xff0c;Spring Cloud做这一层抽象&#x…

oracle数据库中VARCHAR2(50 CHAR) 和VARCHAR2(50) 有啥区别?

VARCHAR2&#xff08;50 char&#xff09;这种类型的字段最多放50个字符&#xff0c;不够50个用空格填充&#xff1b;而VARCHAR2(50)最大允许存放50个字符&#xff0c;但是不足50个也不用空格填充。varchar2是变长字符串&#xff0c;与CHAR类型不同&#xff0c;它不会使用空格填…

《解密小米之互联网下的商业奇迹》

解密小米《解密小米之互联网下的商业奇迹》 磐石之心 清华大学出版社 2014/10/1 书籍&#xff1a;《非同凡响想,乔布斯启示录》 磐石之心&#xff1a;原名王斌&#xff0c;互联网IT资深预言家&#xff0c;第一个提出互联网未来竞争是在线生活方式的竞争&#xff1b;第一个提出3…

计算机内存的故障,计算机内存出现故障的解决方法

内存如果出现故障&#xff0c;会造成系统运行不稳定、程序异常出错和*作系统无法安装的故障&#xff0c;下面将列举内存常见的故障排除实例。1)内存顺序引起的计算机工作不正常故障现象&#xff1a;一台p4计算机&#xff0c;使用的是华硕intel850芯片组的主板&#xff0c;两条r…

2018暑假集训---递推递归----一只小蜜蜂hdu2044

一只小蜜蜂... Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 93249 Accepted Submission(s): 33187Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房&#xff0c;不能反向爬行。请编程计算蜜…

《ASP.NET Core 6框架揭秘》实例演示[28]:自定义一个服务器

作为ASP.NET Core请求处理管道的“龙头”的服务器负责监听和接收请求并最终完成对请求的响应。它将原始的请求上下文描述为相应的特性&#xff08;Feature&#xff09;&#xff0c;并以此将HttpContext上下文创建出来&#xff0c;中间件针对HttpContext上下文的所有操作将借助于…