c# - Code behind control with MVVM pattern? -


i have been asked develop dialog window in wpf contains map control (onlinemapcontrol). map control & associated business logic sits in code behind. i've built control window , written simple view model.

the parent view fires dialog button command in it's view model. know ideally should using idialogservice or equivalent seniors have advised me go .showdialog() in view model command method la:

public showchilddialogcommandaction() {   dialogviewmodel dialogviewmodel = newdialogviewmodel();   dialogview dialogview = new dialogview(dialogviewmodel);    dialogview.showdialog(); } 

this works fine, tight coupling aside. problems arise when i'm trying access properties on child view code behind based map control. dialogviewmodel instance knows nothing of onlinemapcontrol therefore find myself trying chase property value through these classes duplicated property names & getting logical spaghetti in dialogview code behind this.onlinemapcontrol.zoomlevel = this.dialogviewmodel.zoomlevel etc. has bad code smell. how tackle design akin whereby complex control within view accessible through code behind & keep mvvm pattern intact?

if you're doing proper mvvm view model should view's datacontext:

dialogviewmodel dialogviewmodel = newdialogviewmodel(); dialogview dialogview = new dialogview{datacontext = dialogviewmodel}; 

from point on dialogview xaml should use data-binding , shouldn't have code-behind itself, copying data between , dialogviewmodel becomes moot point.

incidentally, service isn't way implement dialog boxes, it's quite easy implement using regular data-binding showed in an article wrote last year.


Comments