当前位置: 首页 > news >正文

WPF 【十月的寒流】学习笔记(1):DataGrid过滤

文章目录

  • 相关链接
  • 代码仓库
  • 前言
  • 环境
  • DataGrid 数据筛选
    • 项目配置
    • 使用原理
    • 主要代码(详细代码可以看我的GitHub仓库)
      • Models.Person
      • DataGirdView
      • DataGridViewModel
    • 实现效果
  • DataGrid直接绑定CollectionView
    • xaml
    • ViewModel
  • 总结

相关链接

十月的寒流

在这里插入图片描述
在这里插入图片描述

在 WPF 中制作 DataGrid 的数据筛选功能

WPF 中如何制作 DataGrid 的分页功能

代码仓库

我为了方便展示源代码,我将代码提交到了代码仓库里面

B站【十月的寒流】对应课程的代码 Github仓库

前言

为了深入的重新学习WPF的基础知识,打算从【B站:十月的寒流】这位大佬上面去学习WPF的相关的知识。我其实更推荐大家去看原视频的相关教程内容。

环境

  • visual studio 2022
  • .net core 8.0
  • windows11

DataGrid 数据筛选

项目配置

如何使用我这里就不展开说明了

WPF CommunityToolkit.Mvvm

WPF CommunityToolkit.Mvvm Messenger通讯

在这里插入图片描述

WPF-UI HandyControl 简单介绍

WPF-UI HandyControl 控件简单实战+IconPacks矢量图导入

在这里插入图片描述

Bogus,.NET生成批量模拟数据
在这里插入图片描述

使用原理

WPF DataGrid 数据过滤

ICollectionView让MVVM更简单

微软官方文档|ICollectionView 接口

在这里插入图片描述
这里不得不提到微软的WPF文档了,写了和没写差不多。

实现原理就是微软为了方便对数据进行分组、排序和筛选。给ItemSorce添加了ICollectionView 专门用于干这个。然后我们可以对ICollectionView添加规则,Ture就是需要,False就是不需要。

主要代码(详细代码可以看我的GitHub仓库)

Models.Person

    public class Person{public int Id { get; set; }public string FullName { get; set; }public string FirstName { get; set; }public string LastName { get; set; }public DateTime BirthDay { get; set; }}

DataGirdView

<UserControl x:Class="DataGrid_Filter.Views.DataGirdView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="clr-namespace:DataGrid_Filter.Views"xmlns:hc="https://handyorg.github.io/handycontrol"xmlns:viewModels="clr-namespace:DataGrid_Filter.ViewModels"mc:Ignorable="d"d:DesignHeight="450"d:DesignWidth="800"><UserControl.DataContext><viewModels:DataGridViewModel  x:Name="ViewModel" /></UserControl.DataContext><DockPanel LastChildFill="True"><StackPanel DockPanel.Dock="Bottom"Orientation="Horizontal"Margin="5"><Button Command="{Binding AddItemCommand}"Content="AddNewItem" /><!--添加名称,方便绑定--><hc:TextBox Text="{Binding FilterStr,UpdateSourceTrigger=PropertyChanged}"hc:InfoElement.Placeholder="Filter By Name"MinWidth="200"Margin="5 0 0 0"x:Name="FilterBox" /></StackPanel><!--添加名称,方便绑定--><DataGrid ItemsSource="{Binding PeopleList}"x:Name="PeopleDataGrid"></DataGrid></DockPanel>
</UserControl>
    public partial class DataGirdView : UserControl{public DataGirdView(){InitializeComponent();//将主要的代码逻辑放在ViewModel里面ViewModel.DataGirdView = this;}}

DataGridViewModel

 public partial class DataGridViewModel : ObservableObject{[ObservableProperty]private string title = "DataGird Tttle";[ObservableProperty]private ObservableCollection<Models.Person> peopleList = new();[ObservableProperty]private ICollectionView collectionView;[ObservableProperty]private string filterStr = "";private DataGrid_Filter.Views.DataGirdView dataGirdView;public DataGrid_Filter.Views.DataGirdView DataGirdView{get => dataGirdView;set {dataGirdView = value;ViewInit();}}public static int OrderId = 1;/// <summary>/// 生成模拟的数据/// </summary>public static Faker<Models.Person> Faker = new Faker<Models.Person>().RuleFor(t => t.Id, f => OrderId++).RuleFor(t => t.FirstName, f => f.Name.FirstName()).RuleFor(t => t.LastName, f => f.Name.LastName()).RuleFor(t => t.FullName, f => f.Name.FullName()).RuleFor(t => t.BirthDay, f => f.Date.Between(new DateTime(1990, 1, 1), new DateTime(2010, 1, 1)));public DataGridViewModel(){PeopleList = new ObservableCollection<Models.Person>(Faker.Generate(10));}[RelayCommand]public void AddItem(){var item = Faker.Generate();PeopleList.Add(item);}public void ViewInit(){//获取ItemSource的CollectionViewCollectionView = CollectionViewSource.GetDefaultView(DataGirdView.PeopleDataGrid.ItemsSource);//给CollectionView添加过滤规则CollectionView.Filter = (item) =>{if (string.IsNullOrEmpty(FilterStr)){return true;}else{var model = item as Models.Person;return model.FirstName.Contains(FilterStr) || model.LastName.Contains(FilterStr) || model.FullName.Contains(FilterStr);}};//在TextChanged的时候,实时更新DataGirdView.FilterBox.TextChanged += (s, e) =>{CollectionView.Refresh();};}}

实现效果

在这里插入图片描述

DataGrid直接绑定CollectionView

第一种方法是通过获取ItemSorce来获取CollectionView,实在是舍近求远。这次我们选择第二种方法。直接绑定设置好的CollectionView。然后我们在每次刷新输入框和改动数据的时候,主动更新绑定内容。

xaml

<UserControl x:Class="DataGrid_Filter.Views.DataGrid2View"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:DataGrid_Filter.Views"xmlns:viewModels="clr-namespace:DataGrid_Filter.ViewModels"xmlns:hc="https://handyorg.github.io/handycontrol"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"><UserControl.DataContext><viewModels:DataGrid2ViewModel /></UserControl.DataContext><Grid><DockPanel LastChildFill="True"><StackPanel DockPanel.Dock="Bottom"Orientation="Horizontal"Margin="5"><Button Command="{Binding AddItemCommand}"Content="AddNewItem" /><!--添加名称,方便绑定--><hc:TextBox Text="{Binding FilterStr,UpdateSourceTrigger=PropertyChanged}"hc:InfoElement.Placeholder="Filter By Name"MinWidth="200"Margin="5 0 0 0"x:Name="FilterBox" /></StackPanel><!--添加名称,方便绑定--><DataGrid ItemsSource="{Binding CollectionView}"x:Name="PeopleDataGrid"></DataGrid></DockPanel></Grid>
</UserControl>

ViewModel

using Bogus;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DataGrid_Filter.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;namespace DataGrid_Filter.ViewModels
{public partial class DataGrid2ViewModel : ObservableObject{[ObservableProperty]private List<Models.Person> people = new List<Models.Person>();[ObservableProperty]private ICollectionView collectionView;private string filterStr = "";public string FilterStr{get{return filterStr;}set{SetProperty(ref filterStr, value);CollectionView.Refresh();}}[RelayCommand]public void AddItem(){var item = DataGridViewModel.Faker.Generate();People.Add(item);CollectionView.Refresh();}public DataGrid2ViewModel(){People = DataGridViewModel.Faker.Generate(10).ToList();CollectionView = CollectionViewSource.GetDefaultView(People);CollectionView.Filter = (item) =>{if (string.IsNullOrEmpty(FilterStr)){return true;}else{var model = item as Models.Person;return model.FirstName.Contains(FilterStr) || model.LastName.Contains(FilterStr) || model.FullName.Contains(FilterStr);}};}}
}

总结

学WPF还是不能闭门造车,一定要先找网上有没有对应的方法,实在没有再自己想方法解决。【十月的寒流】的【WPF教程】确实不错,建议大家都去看一下。

相关文章:

WPF 【十月的寒流】学习笔记(1):DataGrid过滤

文章目录 相关链接代码仓库前言环境DataGrid 数据筛选项目配置使用原理主要代码&#xff08;详细代码可以看我的GitHub仓库&#xff09;Models.PersonDataGirdViewDataGridViewModel 实现效果 DataGrid直接绑定CollectionViewxamlViewModel 总结 相关链接 十月的寒流 在 WPF 中…...

当Vue项目启动后,通过IP地址方式在相同网络段的其他电脑上无法访问前端页面?

当Vue项目启动后&#xff0c;通过IP地址方式在相同网络段的其他电脑上无法访问前端页面&#xff0c;可能是由以下几个原因造成的&#xff1a; 服务监听地址&#xff1a;默认情况下&#xff0c;许多开发服务器&#xff08;如Vue CLI的vue-cli-service serve&#xff09;只监听lo…...

native sql -ABAP开发从入门到精通笔记

Native SQL SQL概要 OPEN SQL读取数据 Select Select <lines> <columns>... Select signle <cols>.... where. 列去重数据 Select distinct <cols>... where... 当取多条数据时&#xff0c;select结果会保存到内表中。 Select ... into...语句的结果不…...

C++完美转发【gpt】

提问&#xff1a;用极其通俗易懂的文字解释C的完美转发 ChatGPT 好的&#xff0c;让我用非常通俗易懂的方式解释一下 C 中的完美转发。 假设你有一个房子&#xff08;函数&#xff09;&#xff0c;你想把家具&#xff08;参数&#xff09;从一个房间&#xff08;函数&#x…...

C++之类和对象(2)

目录 1.类的6个默认成员函数 2. 构造函数 2.1 概念 2.2 特性 3.析构函数 3.1 概念 3.2 特性 4. 拷贝构造函数 4.1 概念 4.2 特征 5.赋值运算符重载 5.1 运算符重载 5.2 赋值运算符重载 2. 赋值运算符只能重载成类的成员函数不能重载成全局函数 3. 用户没有显式实现时&…...

时间序列分析实战(四):Holt-Winters建模及预测

&#x1f349;CSDN小墨&晓末:https://blog.csdn.net/jd1813346972 个人介绍: 研一&#xff5c;统计学&#xff5c;干货分享          擅长Python、Matlab、R等主流编程软件          累计十余项国家级比赛奖项&#xff0c;参与研究经费10w、40w级横向 文…...

Springboot之集成MongoDB无认证与开启认证的配置方式

Springboot之集成MongoDB无认证与开启认证的配置方式 文章目录 Springboot之集成MongoDB无认证与开启认证的配置方式1. application.yml中两种配置方式1. 无认证集成yaml配置2. 有认证集成yaml配置 2. 测试1. 实体类2. 单元测试3. 编写Controller测试 1. application.yml中两种…...

BLEU: a Method for Automatic Evaluation of Machine Translation

文章目录 BLEU: a Method for Automatic Evaluation of Machine Translation背景和意义技术原理考虑 n n n - gram中 n 1 n1 n1 的情况考虑 n n n - gram中 n > 1 n\gt 1 n>1 的情况考虑在文本中的评估初步实验评估和结论统一不同 n n n 值下的评估数值考虑句子长度…...

代码随想录算法训练营|day42

第九章 动态规划 416.分割等和子集代码随想录文章详解 背包类型求解方法0/1背包外循环nums,内循环target,target倒序且target>nums[i]完全背包外循环nums,内循环target,target正序且target>nums[i]组合背包外循环target,内循环nums,target正序且target>nums[i] 416.分…...

vscode与vue/react环境配置

一、下载并安装VScode 安装VScode 官网下载 二、配置node.js环境 安装node.js 官网下载 会自动配置环境变量和安装npm包(npm的作用就是对Node.js依赖的包进行管理)&#xff0c;此时可以执行 node -v 和 npm -v 分别查看node和npm的版本号&#xff1a; 配置系统变量 因为在执…...

Vue前端对请假模块——请假开始时间和请假结束时间的校验处理

开发背景&#xff1a;Vueelement组件开发 业务需求&#xff1a;用户提交请假申请单&#xff0c;请假申请的业务逻辑处理 实现&#xff1a;用户选择开始时间需要大于本地时间&#xff0c;不得大于请假结束时间&#xff0c;请假时长根据每日工作时间实现累加计算 页面布局 在前…...

搭建freqtrade量化交易机器人

本文采用python量化机器人框架 freqtrade 开始操作&#xff01; freqtrade官方文档 官方文档内容过多&#xff0c;请先跟随本文入门阅读&#xff0c;后续深入学习可参考官方文档&#xff5e; 1. 准备云服务器 docker 环境 这里以云服务器选择 ubuntu 系统开始&#xff0c;先…...

php伪协议 [SWPUCTF 2022 新生赛]ez_ez_php(revenge)

打开题目 题目源代码如下 <?php error_reporting(0); if (isset($_GET[file])) {if ( substr($_GET["file"], 0, 3) "php" ) {echo "Nice!!!";include($_GET["file"]);} else {echo "Hacker!!";} }else {highlight_fi…...

1.1_1 计算机网络的概念、功能、组成和分类

文章目录 1.1_1 计算机网络的概念、功能、组成和分类&#xff08;一&#xff09;计算机网络的概念&#xff08;二&#xff09;计算机网络的功能&#xff08;三&#xff09;计算机网络的组成1.组成部分2.工作方式3.功能组成 &#xff08;四&#xff09;计算机网络的分类 总结 1.…...

pytorch中的各种计算

对tensor矩阵的维度变换&#xff0c;加减乘除等是深度学习中的常用操作&#xff0c;本文对一些常用方法进行总结 矩阵乘法 混合矩阵相乘&#xff0c;官网 torch.matmul(input, other, *, outNone) → Tensor这个方法执行矩阵相乘操作&#xff0c;需要第一个矩阵的最后一个维度…...

大数据技术之 Kafka

大数据技术之 Kafka 文章目录 大数据技术之 Kafka第 1 章 Kafka 概述1.1 定义1.2 消息队列1.2.1 传统消息队列的应用场景1.2.2 消息队列的两种模式 1.3 Kafka 基础架构 第 2 章 Kafka 快速入门2.1 安装部署2.1.1 集群规划2.1.2 集群部署2.1.3 集群启停脚本 2.2 Kafka 命令行操作…...

【GB28181】wvp-GB28181-pro部署安装教程(Ubuntu平台)

目录 前言1 安装依赖2 安装MySQL3 安装redis4 编译ZLMediaKit代码及依赖下载编译运行&#xff08;如果要运行wvp整个项目&#xff0c;这步可以先不执行&#xff09; 5 编译wvp-pro下载源码&#xff08;建议从github上下载&#xff0c;gitee上维护有时候不是很同步&#xff09;编…...

CentOS删除除了最近5个JAR程序外的所有指定Java程序

帮我写一个shell脚本&#xff0c;ps -eo pid,lstart,cmd --sort-start_time | grep "pgz-admin"查到的结果&#xff0c;返回的所有进程PID&#xff0c;第六个之上的&#xff0c;全部kill 当然&#xff0c;你可以创建一个简单的Shell脚本来完成这个任务。以下是一个例…...

面试redis篇-13Redis为什么那么快

Redis是纯内存操作,执行速度非常快采用单线程,避免不必要的上下文切换可竞争条件,多线程还要考虑线程安全问题使用I/O多路复用模型,非阻塞IOI/O多路复用模型 Redis是纯内存操作,执行速度非常快,它的性能瓶颈是网络延迟而不是执行速度, I/O多路复用模型主要就是实现了高效…...

python Matplotlib Tkinter--pack 框架案例

环境 python:python-3.12.0-amd64 包: matplotlib 3.8.2 pillow 10.1.0 版本一 import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk import tkinter as tk import tkinter.messagebox as messagebox…...

java_网络服务相关_gateway_nacos_feign区别联系

1. spring-cloud-starter-gateway 作用&#xff1a;作为微服务架构的网关&#xff0c;统一入口&#xff0c;处理所有外部请求。 核心能力&#xff1a; 路由转发&#xff08;基于路径、服务名等&#xff09;过滤器&#xff08;鉴权、限流、日志、Header 处理&#xff09;支持负…...

云启出海,智联未来|阿里云网络「企业出海」系列客户沙龙上海站圆满落地

借阿里云中企出海大会的东风&#xff0c;以**「云启出海&#xff0c;智联未来&#xff5c;打造安全可靠的出海云网络引擎」为主题的阿里云企业出海客户沙龙云网络&安全专场于5.28日下午在上海顺利举办&#xff0c;现场吸引了来自携程、小红书、米哈游、哔哩哔哩、波克城市、…...

IGP(Interior Gateway Protocol,内部网关协议)

IGP&#xff08;Interior Gateway Protocol&#xff0c;内部网关协议&#xff09; 是一种用于在一个自治系统&#xff08;AS&#xff09;内部传递路由信息的路由协议&#xff0c;主要用于在一个组织或机构的内部网络中决定数据包的最佳路径。与用于自治系统之间通信的 EGP&…...

无法与IP建立连接,未能下载VSCode服务器

如题&#xff0c;在远程连接服务器的时候突然遇到了这个提示。 查阅了一圈&#xff0c;发现是VSCode版本自动更新惹的祸&#xff01;&#xff01;&#xff01; 在VSCode的帮助->关于这里发现前几天VSCode自动更新了&#xff0c;我的版本号变成了1.100.3 才导致了远程连接出…...

Python爬虫实战:研究feedparser库相关技术

1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...

大语言模型如何处理长文本?常用文本分割技术详解

为什么需要文本分割? 引言:为什么需要文本分割?一、基础文本分割方法1. 按段落分割(Paragraph Splitting)2. 按句子分割(Sentence Splitting)二、高级文本分割策略3. 重叠分割(Sliding Window)4. 递归分割(Recursive Splitting)三、生产级工具推荐5. 使用LangChain的…...

WordPress插件:AI多语言写作与智能配图、免费AI模型、SEO文章生成

厌倦手动写WordPress文章&#xff1f;AI自动生成&#xff0c;效率提升10倍&#xff01; 支持多语言、自动配图、定时发布&#xff0c;让内容创作更轻松&#xff01; AI内容生成 → 不想每天写文章&#xff1f;AI一键生成高质量内容&#xff01;多语言支持 → 跨境电商必备&am…...

c#开发AI模型对话

AI模型 前面已经介绍了一般AI模型本地部署&#xff0c;直接调用现成的模型数据。这里主要讲述讲接口集成到我们自己的程序中使用方式。 微软提供了ML.NET来开发和使用AI模型&#xff0c;但是目前国内可能使用不多&#xff0c;至少实践例子很少看见。开发训练模型就不介绍了&am…...

精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南

精益数据分析&#xff08;97/126&#xff09;&#xff1a;邮件营销与用户参与度的关键指标优化指南 在数字化营销时代&#xff0c;邮件列表效度、用户参与度和网站性能等指标往往决定着创业公司的增长成败。今天&#xff0c;我们将深入解析邮件打开率、网站可用性、页面参与时…...

.Net Framework 4/C# 关键字(非常用,持续更新...)

一、is 关键字 is 关键字用于检查对象是否于给定类型兼容,如果兼容将返回 true,如果不兼容则返回 false,在进行类型转换前,可以先使用 is 关键字判断对象是否与指定类型兼容,如果兼容才进行转换,这样的转换是安全的。 例如有:首先创建一个字符串对象,然后将字符串对象隐…...