结合上篇帖子进行深入编写,通过使用泛型、反射、委托可实现多表单查询,同时通过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(); } }
}
}
标签:Name,WPFDataGrid,C#,System,查询,Windows,new,using,public
From: https://blog.csdn.net/qq_44774906/article/details/143983620