下面以一个例子说明DataGrid控件的使用方法:
一、程序框架
使用了MVVM Light框架
二、主要代码部分(使用MVVM Light框架):
1.主窗体xaml代码 (MainWindow.xaml):
2.ViewModel类代码(MainViewModel.cs):
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
mylocalDB = new localDB();
QueryCommand = new RelayCommand(Query);
ResetCommand = new RelayCommand(()=>
{
Search = string.Empty;
this.Query();
});
EditCommand = new RelayCommand(t => Edit(t));
DelCommand = new RelayCommand(t => Del(t));
AddCommand = new RelayCommand(Add);
}
localDB mylocalDB;
private string search = string.Empty;
public string Search
{
get { return search; }
set { search = value;RaisePropertyChanged(); }
}
private ObservableCollection gridModelList;
public ObservableCollection GridModelList
{
get { return gridModelList; }
set { gridModelList = value;RaisePropertyChanged(); }
}
#region Command
public RelayCommand QueryCommand { get; set; }
public RelayCommand ResetCommand { get; set; }
public RelayCommand EditCommand { get; set; }
public RelayCommand DelCommand { get; set; }
public RelayCommand AddCommand { get; set; }
#endregion
public void Query()
{
var models = mylocalDB.GetStudentsByName(Search);
GridModelList = new ObservableCollection();
if(models != null)
{
models.ForEach(arg =>
{
GridModelList.Add(arg);
});
}
}
public void Edit(int id)
{
var model = mylocalDB.GetStudentById(id);
if(model != null)
{
UserView view = new UserView(model);
var res = view.ShowDialog();
if(res.Value)
{
var newModel =GridModelList.FirstOrDefault(t=>t.Id == model.Id);
if(newModel != null)
{
newModel.Name = model.Name;
}
}
}
}
public void Add()
{
Student stu = new Student();
UserView view = new UserView(stu);
var res = view.ShowDialog();
if (res.Value)
{
stu.Id = gridModelList.Max(t => t.Id) + 1; ;
mylocalDB.AddStudent(stu);
this.Query();
}
}
public void Del(int id)
{
var model = mylocalDB.GetStudentById(id);
if (model != null)
{
var res = MessageBox.Show($"确认删除当前用户:{model.Name}?","操作提示:",MessageBoxButton.OK,MessageBoxImage.Question);
if(res == MessageBoxResult.OK)
{
mylocalDB.DelStudent(model.Id);
}
this.Query();
}
}
}
3.主窗体后台代码(MainWindow.xaml.cs):
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MainViewModel viewModel = new MainViewModel();
viewModel.Query();
this.DataContext = viewModel;
}
}
4.Student类:
public class Student:ViewModelBase
{
private int id;
private string name;
public int Id
{
get { return id; }
set
{
id = value;
RaisePropertyChanged();
}
}
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged();
}
}
}
5.localDB类(这里充当数据库功能)
public class localDB
{
public localDB()
{
Init();
}
private List students;
private void Init()
{
students = new List();
for (int i = 0; i < 30; i++)
{
students.Add(new Student()
{
Id = i,
Name = $"Sample{i}"
}) ;
}
}
public List GetStudents()
{
return students;
}
//新增
public void AddStudent(Student stu)
{
students.Add(stu);
}
//删除
public void DelStudent(int id)
{
var model = students.FirstOrDefault(t => t.Id == id);
if(model!=null)
{
students.Remove(model);
}
}
public List GetStudentsByName(string name)
{
return students.Where(q=>q.Name.Contains(name)).ToList();
}
public Student GetStudentById(int id)
{
var model = students.FirstOrDefault(t=>t.Id == id);
if(model != null)
{
return new Student() {
Id = model.Id,
Name = model.Name
};
}
return null;
}
}
三、运行效果:
其他示例一:
如果不用MVVM,DataGrid中的按钮也可以Tag来绑定数据,用事件触发:
样式资源:
其他实例二:
(显示部分按钮)
其他实例三:



