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#删除UL LI中指定标签里文字的方法

    C#删除UL LI中指定标签里文字的方法

    这篇文章主要介绍了C#删除UL LI中指定标签里文字的方法,涉及C#针对页面HTML元素进行正则匹配与替换的相关操作技巧,需要的朋友可以参考下
    2017-05-05
  • c#和javascript函数相互调用示例分享

    c#和javascript函数相互调用示例分享

    在webBrowser使用过程中为了C#和JS通讯,webBrowser必须设置ObjectForScripting的属性,它是一个object,这个object可以提供给webBrowser控件载入的网页上的script访问
    2014-01-01
  • 在C#中使用指针的示例代码

    在C#中使用指针的示例代码

    C#向开发人员隐藏了大部分基本内存管理操作,因为它使用了垃圾回收器和引用,但是,有时候我们也需要直接访问内存,例如:进行平台调用,性能优化等等,本文给大家介绍了在C#中使用指针的示例代码,需要的朋友可以参考下
    2024-10-10
  • 在C#中使用Channels的完整教程

    在C#中使用Channels的完整教程

    这篇文章主要介绍了在C#中使用Channels的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Unity实现全屏截图以及QQ截图

    Unity实现全屏截图以及QQ截图

    这篇文章主要为大家详细介绍了Unity实现全屏截图以及QQ截图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • C#的String和StringBuilder详解

    C#的String和StringBuilder详解

    这篇文章主要介绍了C#的String和StringBuilder详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C#编写Windows服务程序详细步骤详解(图文)

    C#编写Windows服务程序详细步骤详解(图文)

    本文介绍了如何用C#创建、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项,需要的朋友可以参考下
    2017-09-09
  • C#排序算法之归并排序

    C#排序算法之归并排序

    这篇文章主要为大家详细介绍了C#排序算法之归并排序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • C# 进行图片压缩的示例代码(对jpg压缩效果最好)

    C# 进行图片压缩的示例代码(对jpg压缩效果最好)

    这篇文章主要介绍了C# 进行图片压缩的示例代码,帮助大家更好的利用c# 处理图片,提高办公效率,感兴趣的朋友可以了解下
    2020-11-11
  • C#条件拼接Expression<Func<T, bool>>的使用

    C#条件拼接Expression<Func<T, bool>>的使用

    本文主要介绍了C#条件拼接Expression<Func<T, bool>>的使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02

最新评论