Sunday, 15 March 2015

wpf - Using MVVM show new window and get updates data -



wpf - Using MVVM show new window and get updates data -

i'm working on wpf mvvm application. i'm showing info in datagrid. i've 2 buttons add together , edit selected record. i've info in viewmodel , i've show window (view) , create sure viewmodels should have no info views. should create view , viewmodel? how info , update datagrid? how can accomplish in mvvm? have not yet decided utilize framework, i've create own interface.

note: ended beingness quite long reply - please inquire me if unclear

the implementation of dialog windows contentious issue in mvvm designs, , different people utilize different approaches.

like you, i've decided not utilize framework , implement things hand. when comes dialog windows, take pragmatic implementation of mvvm, launching dialog window within viewmodel. also, allow each dialog viewmodel have reference window displayed in, can close when appropriate (details below). breaks of strict mvvm "rules", gets job done.

the main downside of might break unit testing if testing goes through dialog. however, can go long way without running problem , has not bothered me yet.

i've built bit of library of dialog viewmodels can extend. it's way much code post here, i'll show highlights.

base viewmodel dialogs

each of dialog windows has viewmodel inherits dialogviewmodelbase, similiar regular viewmodelbase in provides back upwards inotifypropertychanged etc. interesting part public method, phone call wherever launch dialog:

class="lang-cs prettyprint-override">/// <summary> /// creates window instance dialog viewmodel , displays it, getting dialog result. /// </summary> public void showdialogwindow() { // property of dialogviewmodelbase class - thus, each dialogviewmodel holds reference own dialogwindow: this.dialogwindow = new dialogs.views.dialogwindow(); // tell dialogwindow display viewmodel: this.dialogwindow.datacontext = this; // launch window, using method of window baseclass, returns when window closed: this.dialogwindow.showdialog(); }

window launched in above method close when window.dialogresult property set. why dialogwindow property of dialogviewmodelbase class - when subclassing dialog viewmodel wants close dialog window, sets result:

class="lang-cs prettyprint-override">protected void closedialogwithresult(bool dialogwindowresult) { // setting property automatically closes dialog window: this.dialogwindow.dialogresult = dialogwindowresult; }

host window dialog views

the dialogs.views.dialogwindow class showdialogwindow method instantiates defined in xaml , subclass of window. has 2 of import features. first it's primary content element contentcontrol binds current context. allows me define different views different subclasses of dialogviewmodelbase, , dialogwindow host corresponding view based on type of context:

<contentcontrol content="{binding}" /> <!-- in reality within border etc simplified here demonstration -->

the sec of import feature of dialogwindow xaml defines dialog views go dialog viewmodels. here sample:

<window.resources> <!-- default viewmodel-view templates --> <datatemplate datatype="{x:type dialogs:yesnomessageboxdialogviewmodel}"> <views:messageboxview /> </datatemplate> <datatemplate datatype="{x:type dialogs:errordialogviewmodel}"> <views:errordialogview/> </datatemplate> </window.resources>

what does, can define dialogs subclasses dialogviewmodelbase , implement view each, , tell dialogwindow view contentcontrol must show dialog viewmodel.

launching dialog , getting results

below sample 1 of application viewmodels, in launch dialog window allows user select asset type creation:

class="lang-cs prettyprint-override">public void createnewasset() { // instantiate desired dialog viewmodel: dialogs.newassettypeselectiondialogviewmodel dialog = new dialogs.newassettypeselectiondialogviewmodel(); // launch dialog calling method on dialog base of operations class: dialog.showdialogwindow(); // execution halt here until dialog window closes... // user's selection stored in property on dialog viewmodel, , can retrieved: calculatorbase.assettypeenum newassettype = dialog.assettype; switch (newassettype) { // stuff based on user's selection... } }

ps: should write blog entry - when do, post link here, blog entry have more finish code samples.

wpf mvvm interface modal-dialog

No comments:

Post a Comment