c# - How to (correctly) update the M in MVVM of WPF application? -
having passed series of edward tanguay's questions refractoring usage of mvvm wpf app can found in linked sidebar of fat models, skinny viewmodels , dumb views, best mvvm approach?, little confused final wpf application in big smart viewmodels, dumb views, , model, best mvvm approach?
//model public class customer { public string firstname { get; set; } public string lastname { get; set; } public datetime timeofmostrecentactivity { get; set; } public static customer getcurrentcustomer() { return new customer { firstname = "jim" , lastname = "smith" , timeofmostrecentactivity = datetime.now }; } } which returns current user. kind of, beause returns duplicates of newly created "current" user...
but m's data stored , updated in case of need?
suppose, want change model's current user's firstname "gennady"?
i added button updating model button click event handler:
private void button1_click(object sender, routedeventargs e) { } aiming change model's data reflected in gui.
how can this, clicking button... sorry, placing code button1_click()?
or wrong wish it?
then. how correctly update/change m in mvvm ?
update:
answers seem refer should not make changes in m on vm.
though i've asked referenced m-v-vm implementation with:
public customerviewmodel() { _timer = new timer(checkforchangesinmodel, null, 0, 1000); } private void checkforchangesinmodel(object state) { customer currentcustomer = customerviewmodel.getcurrentcustomer(); mapfieldsfrommodeltoviewmodel(currentcustomer, this); } public static void mapfieldsfrommodeltoviewmodel (customer model, customerviewmodel viewmodel) { viewmodel.firstname = model.firstname; viewmodel.lastname = model.lastname; viewmodel.timeofmostrecentactivity = model.timeofmostrecentactivity; } so, example, upon implementing code from adolfo perez's answer changes, textbox's content changed "jim" "gennady" period of interval set in _timer = new timer(checkforchangesinmodel, null, 0, 1000);.
all logic of referenced me m-v-vm in wpf approach such "m" should updated, in order vm has caught changes, not "vm".
even more, cannot understand, if make changes in vm how can reflected in m if vm knows m - not vice versa - model not know viewmodel).
in mvvm should avoid code-behind. reason want end testable classes, in case vm's independent v. run set of unit tests on vm without involving v. hook different types of views without affecting business logic.
your button bind command property icommand property exposed in vm. command in vm handle click event in method specify.
in view:
<button content="change firstname" command="{binding path=changefirstnamecommand"}/> in viewmodel:
//define command public icommand changefirstnamecommand {get;set;} //initialize command in constructor perhaps changefirstnamecommand = new relaycommand(onchangefirstname,canchangefirstname); private void onchangefirstname() { //your firstname textbox in v updated after click button this.firstname = "gennady"; } private bool canchangefirstname() { //add validation set whether button enabled or not. // wpf internals take care of this. return true; } it important keep in mind in pattern v knows vm , vm knows m not other way around.
in example if want change model firstname property woud have following:
- create vm implements
inotifypropertychanged - expose m firstname property in vm notifying changes
create textbox in xaml view , bind text property vm.firstname setting binding mode=twoway.
<textbox text= "{binding path=firstname,mode=twoway,updatesourcetrigger=propertychanged}"/>
as type in textbox firstname directly populated in vm-m. also, 2 way binding, if modify firstname property in vm, change reflected automatically in v
- set view.datacontext vm. sets context data bindings, unless specify different binding source.
- if want persist changes in db inject service class in vm take care of crud operations
take @ simple example:
http://www.codeproject.com/articles/126249/mvvm-pattern-in-wpf-a-simple-tutorial-for-absolute
Comments
Post a Comment