欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF實現(xiàn)帶模糊搜索的DataGrid的示例代碼

 更新時間:2023年02月16日 08:27:53   作者:干杯Archer、  
這篇文章主要為大家詳細介紹了WPF如何實現(xiàn)帶模糊搜索的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 這個庫來創(chuàng)造假數(shù)據(jù)

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--

到此這篇關(guān)于WPF實現(xiàn)帶模糊搜索的DataGrid的示例代碼的文章就介紹到這了,更多相關(guān)WPF模糊搜索DataGrid內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C# SerialPort實現(xiàn)串口通訊的代碼詳解

    C# SerialPort實現(xiàn)串口通訊的代碼詳解

    在.NET平臺下創(chuàng)建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空間是System.IO.Ports,這個新的框架不但可以訪問計算機上的串口,還可以和串口設(shè)備進行通信,本文給大家介紹了C# SerialPort實現(xiàn)串口通訊,需要的朋友可以參考下
    2024-06-06
  • WPF利用TabControl控件實現(xiàn)拖拽排序功能

    WPF利用TabControl控件實現(xiàn)拖拽排序功能

    在UI交互中,拖拽操作是一種非常簡單友好的交互,這篇文章主要為大家介紹了WPF如何利用TabControl控件實現(xiàn)拖拽排序功能,需要的小伙伴可以參考一下
    2023-10-10
  • 關(guān)于C#基礎(chǔ)知識回顧--反射(三)

    關(guān)于C#基礎(chǔ)知識回顧--反射(三)

    在前面例子中,由于MyClass類型的對象是顯示創(chuàng)建的,因此使用反射技術(shù)來調(diào)用MyClass上的方法沒有任何優(yōu)勢--以普通的方式調(diào)用對象上的方法會簡單的多
    2013-07-07
  • LINQ基礎(chǔ)之Join和UNION子句

    LINQ基礎(chǔ)之Join和UNION子句

    這篇文章介紹了LINQ使用Join和UNION子句的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 利用Aspose.Word控件實現(xiàn)Word文檔的操作

    利用Aspose.Word控件實現(xiàn)Word文檔的操作

    偶然一次機會,一個項目的報表功能指定需要導出為Word文檔,因此尋找了很多篇文章,不過多數(shù)介紹的比較簡單一點,于是也參考了官方的幫助介紹,終于滿足了客戶的需求。下面我由淺入深來介紹這個控件在實際業(yè)務(wù)中的使用過程吧
    2013-05-05
  • C#中 const 和 readonly 的不同

    C#中 const 和 readonly 的不同

    const 和 readonly 的區(qū)別,總是不太清楚,于是查了查資料。
    2013-04-04
  • C# 中 TryParse如何將字符串轉(zhuǎn)換為特定類型

    C# 中 TryParse如何將字符串轉(zhuǎn)換為特定類型

    在 C# 中,TryParse 是一個用于將字符串轉(zhuǎn)換為特定類型的方法,它用于嘗試解析字符串并將其轉(zhuǎn)換為指定類型的值,而不會引發(fā)異常,這篇文章主要介紹了C# 中 TryParse 將字符串轉(zhuǎn)換為特定類型的方法,需要的朋友可以參考下
    2024-03-03
  • C# OpenCvSharp利用白平衡技術(shù)實現(xiàn)圖像修復功能

    C# OpenCvSharp利用白平衡技術(shù)實現(xiàn)圖像修復功能

    這篇文章主要為大家詳細介紹了C# OpenCvSharp如何利用白平衡技術(shù)實現(xiàn)圖像修復功能,文中的示例代碼講解詳細,希望對大家有一定的幫助
    2024-02-02
  • C#同步和異步調(diào)用方法實例

    C#同步和異步調(diào)用方法實例

    c#同步和異步很簡單,這里給大家提供一個小例子供大家參考
    2013-11-11
  • C# CSV文件讀寫的實現(xiàn)

    C# CSV文件讀寫的實現(xiàn)

    本文主要介紹了C# CSV文件讀寫的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03

最新評論