C#WPFDataGrid表单查询,利用泛型、反射、委托、可兼容多对象查询

 

结合上篇帖子进行深入编写,通过使用泛型、反射、委托可实现多表单查询,同时通过datagrid绑定List<T>通过查询集合降低对数据库的访问。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using WpfApp2.ViewModels;

namespace WpfApp2
{
    class DataGridQuery<T>
    {

        public static DataGrid Query(List<T> values)
        {
            DataGrid dataGrid = new DataGrid() { AutoGenerateColumns = false };
            Thread.Sleep(100);
            PropertyInfo[] propertyInfos = typeof(Person).GetProperties();
            foreach (var propertyInfo in propertyInfos)
            {
                string text = "";
                if (propertyInfo.GetCustomAttribute<DisplayNameAttribute>() != null)
                {

                    text = propertyInfo.GetCustomAttribute<DisplayNameAttribute>().DisplayName;
                }
                else
                {
                    continue;
                }
                DataGridTemplateColumn Column = new DataGridTemplateColumn();
                StackPanel stack = new StackPanel() { Orientation = Orientation.Vertical };

                TextBlock textBlock = new TextBlock
                {
                    Text = text,
                    VerticalAlignment = VerticalAlignment.Center
                };
                stack.Children.Add(textBlock); // 添加 TextBlock
                TextBox textBox = new TextBox();
                textBox.Name = propertyInfo.Name;
                textBox.KeyDown += new KeyEventHandler((object sender, KeyEventArgs e) =>
                {
                    if (e.Key == Key.Enter)
                    {

                        List<T> values1 = values.Where(
                            p =>
                            {
                                bool matches = false;
                                PropertyInfo[] infos = typeof(Person).GetProperties();
                                foreach (var pro in infos)
                                {
                                    PropertyInfo[] properties = p.GetType().GetProperties();
                                    foreach (var prop in properties)
                                    {
                                        if (textBox.Name == prop.Name)
                                        {
                                            if (textBox.Text.Trim() == "")
                                            {
                                                matches = true;
                                            }
                                            else if (prop.GetValue(p).ToString() == textBox.Text.Trim())
                                            {
                                                matches = true;
                                            }
                                        }
                                    }
                                }
                                return matches;
                            }
                        ).ToList();
                        dataGrid.ItemsSource = values1;
                    }
                });
                stack.Children.Add(textBox); // 添加 TextBlock

                // 设置列头模板
                Column.Header = stack;
                // 创建单元格模板
                DataTemplate cellTemplate = new DataTemplate();
                FrameworkElementFactory cellTextBlock = new FrameworkElementFactory(typeof(TextBlock));
                cellTextBlock.SetBinding(TextBlock.TextProperty, new System.Windows.Data.Binding(text));
                cellTemplate.VisualTree = cellTextBlock;

                Column.CellTemplate = cellTemplate;
                dataGrid.Columns.Add(new DataGridTextColumn { Header = stack, Binding = new System.Windows.Data.Binding(propertyInfo.Name) });

            }
            dataGrid.ItemsSource = values;
            return dataGrid;
        }
    }
}

该段代码主要是定义了MVVM框架下的UI与数据分离,同时实现数据订阅的功能。 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Xml.Linq;
using WpfApp2.MVVM;

namespace WpfApp2.ViewModels
{
    public class MainWindow : ViewModelBase
    {
        private List<Person> people;
        public List<Person> People { get { return people; } set { people = value; OnPropertyChanged(); } }
        private DataGrid dataGrid = new DataGrid() { AutoGenerateColumns = false };
        public DataGrid DataGrid { get { return dataGrid; } set { dataGrid = value; OnPropertyChanged(); } }
        public MainWindow()
        {
            People = new List<Person>
            {
                new Person{Id=1,Name="张三asdfasfasdfadfafdasd",Age =25 },
                new Person{Id=2,Name="李四",Age =21 },
                new Person{Id=3,Name="王五",Age =23 },
                new Person{Id=3,Name="王五",Age =23 }
            };
            DataGrid = DataGridQuery<Person>.Query(People);
        }
    }

    public class Person : ViewModelBase
    {
        private int id;
        private string name;
        private int age;
        [DisplayName("序号")]
        public int Id { get { return id; } set { id = value; OnPropertyChanged(); } }
        [DisplayName("姓名")]
        public string Name { get { return name; } set { name = value; OnPropertyChanged(); } }
        [DisplayName("年龄")]
        public int Age { get { return age; } set { age = value; OnPropertyChanged(); } }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值