WPF+MVVM实现datagrid绑定(2)
程序员文章站
2022-07-13 23:33:46
...
3、BaseViewModel
属性改变通知类,多个对象都要使用到。
namespace WPF_MVVM_Demo1.ViewModel
{
public class BaseViewModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (propertyName != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
4、ViewModel类
该类真正的结合Model和View层的一个类,内容主要就在这里。
1、定义了数据源和List集合。
/// <summary>
/// 可以为数据库传过来的数据
/// </summary>
List<User> mylist = new List<User>();
/// <summary>
///
/// </summary>
ObservableCollection<User> _mylist = new ObservableCollection<User>();
/// <summary>
/// 绑定Datagrid的数据源
/// </summary>
public ObservableCollection<User> ShowList
{
get { return _mylist; }
set
{
_mylist = value;
RaisePropertyChanged("mylist");
}
}
2、事件绑定
这里是定义,后面初始化的时候,需要初始化对应的函数(方法):
/// <summary>
/// 添加事件绑定
/// </summary>
public BaseCommands AddCommand { get; set; }
/// <summary>
/// 修改事件绑定
/// </summary>
public BaseCommands UpdateCommand { get; set; }
/// <summary>
/// 删除事件绑定
/// </summary>
public BaseCommands DeleteCommand { get; set; }
3、属性绑定
属性之所以这么写是为了选择datagrid的不同行的时候,通知界面数据发生变化。
public int _id;
public int ID
{
get { return _id; }
set
{
_id = value;
RaisePropertyChanged("ID");
}
}
public string _name;
public string Name
{
get => _name;
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
public int _age;
public int Age
{ get=>_age;
set
{
_age = value;
RaisePropertyChanged("Age");
}
}
public string _sex;
public string Sex { get=> _sex; set
{
_sex = value;
RaisePropertyChanged("Sex");
} }
public string _remark;
public string Remarks { get=>_remark; set
{
_remark = value;
RaisePropertyChanged("Remarks");
} }
5、构造函数
构造函数除了初始化绑定事件,另一个就是初始化数据,当然数据也可以从数据库获取,绑定到list集合即可。
//构造函数
public UserViewModel()
{
InitData();
}
void InitData()
{
AddCommand = new BaseCommands();
AddCommand.ExecuteCommand = new Action<object>(addStudent);
UpdateCommand = new BaseCommands();
UpdateCommand.ExecuteCommand = new Action<object>(updateStudent);//修改方法
DeleteCommand = new BaseCommands();
DeleteCommand.ExecuteCommand = new Action<object>(deleteStudent);//修改方法
//SelectCommand = new BaseCommands();
//SelectCommand.ExecuteCommand = new Action<object>(SelectionChangedCommand);
mylist.Add(new User() { ID = 1, Name = "张三", Age = 20, Sex = "女", Remarks = "无" });
mylist.Add(new User() { ID = 2, Name = "李四", Age = 21, Sex = "女", Remarks = "无" });
mylist.Add(new User() { ID = 3, Name = "王五", Age = 22, Sex = "女", Remarks = "无" });
mylist.Add(new User() { ID = 4, Name = "赵六", Age = 24, Sex = "女", Remarks = "无" });
Binding();
}
6、Binding绑定
这个函数就是过滤数据之后的绑定,后续可以结合分页、带条件查询处理等都可以在这里处理。
private void Binding()
{
ShowList.Clear();
mylist.ToList().ForEach(p => ShowList.Add(p));
}
7、添加和修改的实现
public void addStudent(Object parameter)
{
int id = mylist[mylist.Count() - 1].ID;
mylist.Add(new User() { ID = id + 1, Name = Name, Age = Age, Sex = Sex, Remarks = Remarks });
Binding();
}
public void updateStudent(object parameter)
{
if (ID == 0)
{
MessageBox.Show("请选择修改项");
return;
}
foreach (var item in mylist)
{
if (item.ID == ID)
{
item.ID = ID;
item.Name = Name;
item.Sex = Sex;
item.Remarks = Remarks;
item.Age = Age;
break;
}
}
Binding();
}
public void deleteStudent(object parameter)
{
if (ID == 0)
{
MessageBox.Show("请选择删除项");
return;
}
User user1 = mylist.Single(p => p.ID == ID);
mylist.Remove(user1);
Binding();
}
8、dagarid的选择事件
这个即上面说的为什么定义了两个,这里用到的就是泛型类,不仅可以拿到选择的dagagird的数据,同时将数据显示在界面上。
public ICommand SelectionChangedCommand
{
get
{
return new BaseCommands<DataGrid>((datagrid) =>BtnClick(datagrid));
}
}
public void BtnClick(object obj)
{
DataGrid datagrid = (DataGrid)obj;
if (datagrid.SelectedItems.Count > 0)
{
var user = (User)datagrid.SelectedItems[0];
ID = user.ID;
Name = user.Name;
Age = user.Age;
Sex = user.Sex;
Remarks = user.Remarks;
}
}
以上是关于ViewModel的介绍。