3) view model layer dynamicdataviewmodel. CS
Using system;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. Ink;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Using mappstructure. datasource;
Using system. componentmodel;
Using system. Collections. Generic;
Using mentities;
Namespace mappstructure. viewmodel
{
/// <Summary>
/// View model layer. Note that the inotifypropertychanged interface must be implemented so that the VM layer can be bound.
/// Viewmodel can be used as a base class. This base class mainly implements the inotifypropertychanged interface.
/// And some public methods you need. For simplicity, no base class is implemented.
/// </Summary>
Public class dynamicdataviewmodel: inotifypropertychanged
{
/// <Summary>
/// The model layer service instance used in the view model layer. Note that
/// You can call multiple service instances at the model layer. Here is the demonstration, so only one instance is used.
/// </Summary>
Private dynamicdatasource thes;
Public dynamicdataviewmodel ()
{
Thes = new dynamicdatasource ();
// Initialize and register the command.
Commands = new dictionary <string, mycommand> ();
Commands. Add ("button1command", new mycommand (onbuttoncommand ));
}
Private void onbuttoncommand (object parameter)
{
Loaddata ();
MessageBox. Show ("OK ");
}
/// <Summary>
/// Command dictionary set. The first advantage is that it can reduce the hard code for defining commands and provide a dynamic
/// The possibility of State commands, and is conducive to expansion.
/// </Summary>
Public dictionary <string, mycommand> commands
{
Get;
Private set;
}
Private list <dynamicdatarow> _ datasource;
Public list <dynamicdatarow> datasource
{
Get
{
Return _ datasource;
}
Private set
{
_ Datasource = value;
Raisepropertychanged ("datasource ");
}
}
Private dynamicdatatable _ datatable;
/// <Summary>
/// Obtain the dynamic data table.
/// </Summary>
Public dynamicdatatable datatable
{
Get
{
Return _ datatable;
}
Private set
{
_ Datatable = value;
Raisepropertychanged ("datatable ");
}
}
/// <Summary>
/// Data loading.
/// </Summary>
Private void loaddata ()
{
Thes. getdynamicdatatable ("select * From employeeinfo", op =>
{
If (op. haserror = false)
{
Datasource = op. value. Rows;
Datatable = op. value;
}
Else
{
MessageBox. Show (op. errormsg );
}
}, Null );
}
Public event propertychangedeventhandler propertychanged;
Protected void raisepropertychanged (string propertyname)
{
VaR handler = propertychanged;
If (handler! = NULL)
{
Handler (this, new propertychangedeventargs (propertyname ));
}
}
}
/// <Summary>
/// Your own command class, mainly for command binding. Here is a typical command mode, and the command receiver is the local VM.
/// The command receiver does not act as a member of the command, but uses the delegate method, which is more convenient in this case.
/// </Summary>
Public class mycommand: icommand
{
Public bool canexecute (object parameter)
{
Return true;
}
Private action <object >_action;
Public mycommand (Action <Object> action)
{
This. _ Action = action;
}
Public event eventhandler canexecutechanged;
Public void execute (object parameter)
{
If (_ Action! = NULL)
{
_ Action (parameter );
}
}
}
}
Note that my commands are not defined in a general way, but in a dictionary set. The benefits are described above. Please pay attention to the page binding method.