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.pswp.cn/news/282675.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/282675.shtml
英文地址,請注明出處:http://en.pswp.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上下文的所有操作將借助于…