WPF实现带模糊搜索的DataGrid的示例代码

 更新时间:2023年02月16日 08:27:53   作者:干杯Archer、  
这篇文章主要为大家详细介绍了WPF如何实现带模糊搜索的DataGrid,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下

带模糊搜索的DataGrid

前端代码 view

<Window
    x:Class="MVVM.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:hc="https://handyorg.github.io/handycontrol"
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:prism="http://prismlibrary.com/"
    Title="{Binding Title}"
    Width="525"
    Height="350"
    prism:ViewModelLocator.AutoWireViewModel="True">
    <Grid>
        <DataGrid
            Name="dataGrid"
            AutoGenerateColumns="False"
            CanUserDeleteRows="True"
            ItemsSource="{Binding CollectionView}">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Id}" Header="Id" />
                <DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName" />
                <DataGridTextColumn Binding="{Binding LastName}" Header="LastName" />
                <DataGridTextColumn Binding="{Binding Birthday}" Header="Birthday" />
                <DataGridTextColumn Binding="{Binding Salay}" Header="Salay" />
            </DataGrid.Columns>
        </DataGrid>
        <Grid VerticalAlignment="Bottom">
            <StackPanel Orientation="Horizontal">
                <Button
                    Width="120"
                    Command="{Binding AddEmployeeCommand}"
                    Content="New Employee" />
                <hc:TextBox
                    Name="filterTextBox"
                    Width="200"
                    Margin="5,0"
                    hc:InfoElement.Placeholder="Filter data by name"
                    Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}" />
            </StackPanel>
        </Grid>
    </Grid>
</Window>

后端代码 ViewModel

using Prism.Commands;
using Prism.Mvvm;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
using 带筛选的DataGrid.Core;

namespace MVVM.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public MainWindowViewModel()
        {
            AddEmployeeCommand = new DelegateCommand(AddEmployee);
            this.employees = new List<Employee>(Employee.FakeMany(10));
            CollectionView = CollectionViewSource.GetDefaultView(employees);
            CollectionView.Filter = (item) =>
            {
                if (string.IsNullOrEmpty(FilterText)) return true;
                var em = item as Employee;
                return em.FirstName.Contains(FilterText) || em.LastName.Contains(FilterText);
            };
        }

        List<Employee> employees;
        public DelegateCommand AddEmployeeCommand { get; set; }

        private ICollectionView collectionView;
        public ICollectionView CollectionView
        {
            get { return collectionView; }
            set { SetProperty(ref collectionView, value); }
        }

        private string filterText;
        public string FilterText
        {
            get { return filterText; }
            set { SetProperty(ref filterText, value,OnFilterTextChanged); }
        }

        public void OnFilterTextChanged()
        {
            CollectionView.Refresh();
        }

        public void AddEmployee()
        {
            employees.Add(Employee.FakeOne());
            CollectionView.Refresh();
        }
    }
}

Model 代码,引用了 Faker 这个库来创造假数据

using Bogus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 带筛选的DataGrid.Core
{
    public class Employee
    {
        public int Id { get; set; }
        public string  FirstName { get; set; }
        public string LastName { get; set; }
        public DateOnly Birthday { get; set; }
        public int Salay { get; set; }

        public static Employee FakeOne() => employeeFaker.Generate();

        public static IEnumerable< Employee> FakeMany(int count ) => employeeFaker.Generate(count);


        private static readonly Faker<Employee> employeeFaker = new Faker<Employee>()
            .RuleFor(x => x.Id, x => x.IndexFaker)
            .RuleFor(x => x.FirstName, x => x.Person.FirstName)
            .RuleFor(x => x.LastName, x => x.Person.LastName)
            .RuleFor(x => x.Birthday, x => DateOnly.FromDateTime(x.Person.DateOfBirth))
            .RuleFor(x => x.Salay, x => x.Random.Int(6, 30) * 1000);
    }
}

代码:https://github.com/sw554227643/---DataGrid-MVVM--

到此这篇关于WPF实现带模糊搜索的DataGrid的示例代码的文章就介绍到这了,更多相关WPF模糊搜索DataGrid内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • c#日志记录帮助类分享

    c#日志记录帮助类分享

    这篇文章主要介绍了c#日志记录帮助类,可以设置记录的日志类型,需要的朋友可以参考下
    2014-03-03
  • c#开发的程序安装时动态指定windows服务名称

    c#开发的程序安装时动态指定windows服务名称

    前段时间由于项目的需求,要在Windows里把同样的组件制作成多个不同名称的服务,这些服务完成类似的功能,仅需要修改业务配置文件
    2012-06-06
  • C#中LINQ to Objects查询的实现

    C#中LINQ to Objects查询的实现

    LINQ to Objects是LINQ技术在C#中的一种应用,它专门用于对内存中的对象集合进行查询和操作,本文就详细的介绍C#中LINQ to Objects查询的实现,感兴趣的可以了解一下
    2023-08-08
  • C#在foreach遍历删除集合中元素的三种实现方法

    C#在foreach遍历删除集合中元素的三种实现方法

    这篇文章主要给大家总结介绍了关于C#在foreach遍历删除集合中元素的实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-12-12
  • C# 一个WCF简单实例

    C# 一个WCF简单实例

    以订票为例简单应用wcf程序,需要的朋友可以参考下
    2012-10-10
  • C#如何实现dataGridView动态绑定数据

    C#如何实现dataGridView动态绑定数据

    这篇文章主要介绍了C#如何实现dataGridView动态绑定数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • 使用C#实现基于TCP和UDP协议的网络通信程序的基本示例

    使用C#实现基于TCP和UDP协议的网络通信程序的基本示例

    这篇文章主要介绍了使用C#实现基于TCP和UDP协议的网络通信程序的示例,文中分别编写了基本的服务器端和客户端,代码十分简单,需要的朋友可以参考下
    2016-04-04
  • C#中实现查找mysql的安装路径

    C#中实现查找mysql的安装路径

    这篇文章主要介绍了C#中实现查找mysql的安装路径,本文讲解使用SQL语句查询出mysql的安装路径,方便在备份时使用,需要的朋友可以参考下
    2015-06-06
  • C#中IDispose接口的实现及为何这么实现详解

    C#中IDispose接口的实现及为何这么实现详解

    这篇文章主要给大家介绍了关于C#中IDispose接口的实现及为何这么实现的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-05-05
  • 详解C#如何在不同工作簿之间复制选定单元格区域

    详解C#如何在不同工作簿之间复制选定单元格区域

    处理Excel文档时,我们经常需要将数据整合到一个工作表以便于我们进行管理或数据对比。本文将演示如何通过编程方式将选定的单元格区域从一个工作簿复制到另一个工作簿
    2023-02-02

最新评论