快速分頁wpf

/*沒有在xaml設置上下文window.context是因為 命名空間一直對應不上  所以在xaml.cs
里面綁定*/
<Window x:Class="DataGrid.views.MainWindow"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:local="clr-namespace:DataGrid"xmlns:AutoGrid="clr-namespace:WpfAutoGrid"xmlns:viewmodels="clr-namespace:DataGrid.viewmodels"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><AutoGrid:AutoGrid ColumnCount="2" RowCount="2"  Rows="5*,*" Columns="*,*" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"><DataGrid Grid.ColumnSpan="2" ItemsSource="{Binding Itemsview}"></DataGrid><TextBox HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Text="{Binding Pagenum}" ></TextBox><Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Command="{Binding NextPageCommand}">Next</Button></AutoGrid:AutoGrid></Grid>
</Window>

using Bogus;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using Bogus;
using System.Collections.ObjectModel;namespace DataGrid.viewmodels
{public class student   //類變量要是屬性getset{public int Id { get; set; }public string Name { get; set; }public double Score { get; set; }}public class StudentDataGenerator{public static ObservableCollection<student> GenerateStudents(int count){var fakeStudents = new Faker<student>().RuleFor(s => s.Id, f => f.IndexGlobal + 1)  // ID從1開始.RuleFor(s => s.Name, f => f.Name.FullName())    // 隨機姓名.RuleFor(s => s.Score, f => f.Random.Double(0, 100)) // 0-100的隨機分數.Generate(count);  // 生成指定數量的學生return new ObservableCollection<student>(fakeStudents);}}  /*bogus生成數據*/public partial class DataControl:ObservableObject /*這里一定要partial*/{private CollectionView _itemsview;private const int _pagesize = 10;private int _pagenum = 1;public CollectionView Itemsview { get { return _itemsview; } }public int Pagenum{get { return _pagenum; }set { SetProperty(ref _pagenum, value); Itemsview?.Refresh(); }}public int TotalPages => (int)Math.Ceiling(Students.Count / (double)_pagesize);[ObservableProperty]ObservableCollection<student> _students;[RelayCommand]public void NextPage(){if (_pagenum < TotalPages) // 如果還有下一頁{Pagenum++;   //這里一定只能改屬性  }}[RelayCommand]     //relaycommand生成的命令會在函數后面加上commandpublic void PrevPage(){if (_pagenum > 1) // 如果不是第1頁{Pagenum--;}}//原理就是_itemsview匹配數據 過濾之后留下頁碼是1的內容 返回true的內容會被存入_itemsviewpublic DataControl(ObservableCollection<student> stus) //有參構造函數傳數據{this._students = stus;    //這里要用observablecollection 不能用enumerable_itemsview = (CollectionView)CollectionViewSource.GetDefaultView(this._students);_itemsview.Filter = (item) =>{var index = Students.IndexOf((student)item); int itemPage = (index / _pagesize) + 1;return itemPage == Pagenum; // 只顯示當前頁的數據;};}}}

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataGrid.viewmodels;namespace DataGrid.views
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new DataControl(StudentDataGenerator.GenerateStudents(36));}}
}//xaml不好綁定viewmodel  就只能到cs里面綁定了  this.datacontext = new ........
//壞處就是xaml不會有提示

sqlsugar操作

using SqlSugar;
using System;public static class IntDbHelper
{private static SqlSugarScope Connection => new SqlSugarScope(new ConnectionConfig(){ConnectionString = "Data Source=./data.db",DbType = DbType.Sqlite,IsAutoCloseConnection = true});// -------------------- 針對int值的操作 --------------------// 插入數據(返回新增的ID)public static int Insert(string tableName, Dictionary<string, object> values){return Connection.Insertable(values).AS(tableName).ExecuteReturnIdentity();}// 查詢數據(返回單個int值)public static int GetInt(string tableName, string field, int id){return Connection.Queryable<dynamic>().AS(tableName).Where($"Id = {id}").Select(field).First();}// 更新數據(更新指定int字段)public static bool UpdateInt(string tableName, int id, string field, int newValue){return Connection.Updateable<dynamic>().AS(tableName).SetColumns($"{field} = {newValue}").Where($"Id = {id}").ExecuteCommand() > 0;}// 刪除數據(根據int ID)public static bool Delete(string tableName, int id){return Connection.Deleteable<dynamic>().AS(tableName).Where($"Id = {id}").ExecuteCommand() > 0;}
}

?int step = 0;

var result = from person in people

? ? ? ? ? ? ?let currentId = step

? ? ? ? ? ? ?select new

? ? ? ? ? ? ?{

? ? ? ? ? ? ? ? ?Id = (step += 2) - 2, // 0, 2, 4...

? ? ? ? ? ? ? ? ?Person = person

? ? ? ? ? ? ?};

不用直接操作id? 可以linq給序號??

?

?

linq里面只能OfType<student>() 不能強轉

datagrid 返回的 selecteditems 是IList類型的

?

CollectionView 方式

  • 優點:自動處理分頁,刪除后自動回填
  • 缺點:需要調用?Refresh()?刷新視圖

會自動回填

?

linq加反射則不會自動回填



Dictionary<int, string> dict = new Dictionary<int, string>();

dict[1] = "Apple"; // 鍵 1 不存在,自動添加鍵值對 {1: "Apple"}

dict[2] = "Banana"; // 鍵 2 不存在,自動添加鍵值對 {2: "Banana"}

dict[1] = "Avocado"; // 鍵 1 已存在,直接覆蓋值,變成 {1: "Avocado"}

List<int> keyList = dict.Keys.ToList(); // 轉為 List<int>

int[] keyArray = dict.Keys.ToArray(); // 轉為 int[]

var longNameStudents = from s in students

? ? ? ? ? ? ? ? ? ? ? let nameLength = s.Name.Length // 定義臨時變量

? ? ? ? ? ? ? ? ? ? ? where nameLength > 5 // 復用計算結果

? ? ? ? ? ? ? ? ? ? ? select new { s.Name, nameLength };

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/914483.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/914483.shtml
英文地址,請注明出處:http://en.pswp.cn/news/914483.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何徹底禁用 Chrome 自動更新

如何徹底禁用 Chrome 自動更新 隨著谷歌將 Chrome 瀏覽器版本升級至 138&#xff0c;它即將徹底拋棄對 Manifest V2 擴展的支持。許多用戶希望將瀏覽器版本鎖定在 138&#xff0c;以繼續使用 uBlock Origin、Tampermonkey 等常用擴展。 本文總結了四種有效方法&#xff0c;幫助…

流批一體的“奧卡姆剃刀”:Apache Cloudberry 增量物化視圖應用解析

引言&#xff1a;流批一體&#xff0c;理想與現實的鴻溝 在數據驅動的今天&#xff0c;“實時”二字仿佛擁有魔力&#xff0c;驅使著無數企業投身于流批一體架構的建設浪潮中。我們渴望實時洞察業務變化&#xff0c;實時響應用戶需求。以 Apache Flink 為代表的流處理引擎&…

C# 入門教程(三):詳解字段、屬性、索引器及各類參數與擴展方法

文章目錄一、字段、屬性、索引器、常量1.字段2.屬性2.1 什么是屬性2.2 屬性的聲明2.3 屬性與字段的關系3 索引器4. 常量二、傳值 輸出 引用 數組 具名 可選參數&#xff0c;擴展方法2.1 傳值參數2.1.1 值類型 傳參2.1.2 引用類型 傳參2.2 引用參數2.2.1 引用參數-值類型 傳參2.…

《美術教育研究》是什么級別的期刊?是正規期刊嗎?能評職稱嗎?

?問題解答&#xff1a;問&#xff1a;《美術教育研究》是不是核心期刊&#xff1f;答&#xff1a;不是&#xff0c;是知網收錄的第一批認定學術期刊。問&#xff1a;《美術教育研究》級別&#xff1f;答&#xff1a;省級。主管單位&#xff1a; 安徽出版集團有限責任公司 主辦…

每日算法刷題Day47:7.13:leetcode 復習完滑動窗口一章,用時2h30min

思考: 遇到子數組/子字符串可以考慮能不能用滑動窗口&#xff0c; 定長:逆向思維,答案不定 最大長度/最小長度:一般求長度 越長越合法/越短越合法/恰好:一般求數量 主要思考窗口條件成立&#xff0c; 判斷條件是符合窗口條件(最小長度/越長越合法還是不符合(最大長度/越短越合法…

電流驅動和電壓驅動的區別

理解電流驅動和電壓驅動的區別對電路設計至關重要&#xff0c;尤其在高速、高抗噪要求的場景&#xff08;如LVDS&#xff09;。以下是兩者的核心對比&#xff1a;一、電壓驅動 (Voltage Drive) 核心原理&#xff1a; 驅動器輸出一個受控的電壓&#xff08;與負載阻抗無關&#…

宿舍電費查詢——以ZUA為例

宿舍電費查詢——以ZUA為例0. 安裝抓包環境手機端桌面端1. 登錄1.1 開啟抓包后進入繳費頁面&#xff1a;1.2 分析請求1.3 編寫登錄代碼2. 獲取樓棟及房間ID2.1 獲取樓棟ID2.2 編寫獲取樓棟ID代碼2.3 獲取房間ID2.4 編寫獲取房間ID代碼3. 獲取剩余電費&#xff1a;3.1 選擇房間號…

vue中計算屬性的介紹

Vue.js 中的計算屬性是基于它的響應式系統來實現的&#xff0c;它可以根據 Vue 實例的數據狀態來動態計算出新的屬性值。在 Vue 組件中&#xff0c;計算屬性常用于對數據進行處理和轉換&#xff0c;以及動態生成一些需要的數據。一、使用方式1.定義計算屬性&#xff1a; 在Vue組…

MFC UI控件CheckBox從專家到小白

文章目錄CheckBox勾選框控件控件與變量綁定控件點擊消息映射互斥CheckBox勾選框控件 控件與變量綁定 方案一&#xff1a; BOOL m_bEnable1; BOOL m_bEnable2; void A::DoDataExchange(CDataExchange* pDX) {DDX_Check(pDX, IDC_CK_1, m_bEnable1);DDX_Check(pDX, IDC_CK_2, …

阿爾卡特ACT 250 ATP 150 AND ATP 400 分子泵控制器TURBOMOLECULAR PUMP CONTROLLER ALCATEL

阿爾卡特ACT 250 ATP 150 AND ATP 400 分子泵控制器TURBOMOLECULAR PUMP CONTROLLER ALCATEL

python的小學課外綜合管理系統

前端開發框架:vue.js 數據庫 mysql 版本不限 后端語言框架支持&#xff1a; 1 java(SSM/springboot)-idea/eclipse 2.NodejsVue.js -vscode 3.python(flask/django)–pycharm/vscode 4.php(thinkphp/laravel)-hbuilderx 數據庫工具&#xff1a;Navicat/SQLyog等都可以 摘要 隨著…

實用技巧 Excel 與 XML互轉

一 概述 在android多語言適配中&#xff0c;可能提供的是excel格式的多語言翻譯&#xff0c;而且翻譯數量非常龐大。那手動一個一個往xml里面添加效率非常低&#xff0c;這時候就需要把excel快速轉為android可以直接用的資源文件string.xml二 轉換流程2.1 第一步任意文件夾或者…

云原生技術與應用-Containerd容器技術詳解

目錄 一.Containerd概述 1.什么是containerd 2.Containerd的起源與背景 二.Containerd架構 1.Containerd架構概述 2.核心組件解析 三.安裝配置Containerd 1.安裝Containerd 2.配置Containerd 四.Containerd基本操作 1.鏡像類操作 2.容器類操作 3.任務類操作 4.其他操作 一.…

LINUX714 自動掛載/nfs;物理卷

開機自動掛載 /etc/fstab vim /etc/fstab /dev/sdb2 /u2 ext4 defaults 0 0 mount -a [rootweb ~]# vim /etc/fstab [rootweb ~]# cat /etc/fstab# # /etc/fstab # Created by anaconda on Sat Apr 19 17:11:28 2025 # # Accessible filesystems, by reference, are maintai…

系統性學習C語言-第十六講-深入理解指針(6)

系統性學習C語言-第十六講-深入理解指針&#xff08;6&#xff09;1. sizeof 和 strlen 的對比1.1 sizeof 1.2 strlen 1.3 sizeof 和 strlen 的對比2. 數組和指針筆試題解析2.1 一維數組2.2 字符數組2.3 二維數組3. 指針運算筆試題解析3.1 題目1&#xff1a;3.2 題目…

8:從USB攝像頭把聲音拿出來--ALSA大佬登場!

前言前面的章節我們從認識攝像頭開始&#xff0c;逐漸認識的YCbCr&#xff0c;并對其進行了H264的編碼以及MP4封裝。整個過程中&#xff0c;我們大致使用了V4L2和FFmpeg這兩個重量級工具&#xff0c;就像我們前面章節所講&#xff0c;V4L2只是給圖像做服務的&#xff0c;并不參…

Linux 命令:useradd

Linux useradd 命令詳細教程 useradd 是 Linux 系統中用于創建新用戶賬戶的基礎命令&#xff0c;它通過配置文件&#xff08;如 /etc/passwd、/etc/shadow&#xff09;和默認設置自動完成用戶創建流程。本文將詳細介紹其用法、參數及相關配置。資料已經分類整理好&#xff1a;h…

Pytest之收集用例規則與運行指定用例

&#x1f345; 點擊文末小卡片&#xff0c;免費獲取軟件測試全套資料&#xff0c;資料在手&#xff0c;漲薪更快 小伙伴們大家好呀&#xff0c;今天筆者會給大家講解一下pytest是如何收集我們寫好的用例&#xff1f;我們又有哪些方式來運行單個用例或者批量運行用例呢&#xff…

qt 使用memcpy進行內存拷貝時注意的問題

int offset sizeof(st_target_data);// 預先分配足夠空間this->featureData.resize(offsetsize);// 再執行拷貝memcpy(this->featureData.data()offset, dataa, size);注意 一定要在mencpy之前 使用resize分配足夠的空間&#xff0c;否則在方法退出時候會閃退&#xff…

微調性能趕不上提示工程怎么辦?Can Gradient Descent Simulate Prompting?——論文閱讀筆記

今天速讀一篇文章 Can Gradient Descent Simulate Prompting? 一句話總結 針對【新知識應用的場景里&#xff0c;FT效果往往追不上ICL】這個情況&#xff0c;作者引入MAML的思想↓ 內圈讓模型學習新知識形成知識FT模型&#xff1b; 外圈通過最小化ICL和知識FT模型的KL散度&…