c# - Open new View when new ViewModel is available? -
i relatively new wpf/mvvm. our current wpf project still prototype application, means haven't come designing of main layout. have designed reports (by reports, mean quite complicated usercontrol
each of has configuration controls such comobbox
or textbox
, , datagrid
, graph
controls data want present) view
and viewmodel
. still prototype, need open new window
displays of these usercontrol
. in future, might change locate different usercontrol
in different regions inside main window, layout of visual studio. main point is, our application include hundred of such usercontrol
or call reports. different previous working projects had static layout/mainview.
i still haven't figured out architecture it. our classic usage scenario let user select in menu report open, , have command
(either in mainviewmodel, or in viewmodel of report) objects open new report. command
generating new viewmodel
(viewmodel
first case) , corresponding view
should generated (by whom?), , new window
should opened including newly-generated usercontrol
.
i guess need 2 services:
a service subscribes new-viewmodel-generated event, , resolve
usercontrol
when such event happens.(for our current prototype application) window manager, subscribes new-usercontrol-generated event published 1) service, , opens new window display it.
and in future our actual application, can change 2) service , put them different regions. second, simple , temporary, can have 1 object in code-behind of mainview, subscribes event using eventaggregator
, , generate new window
. correct ?
can tell me how should achieve this?
data binding can handle you. in container want display reports add contentcontrol
, bind property holds vm report want display currently.
<window> <contentcontrol content="{binding path=currentreport}" /> </window>
to display different reports wrap each of usercontrols
in own datatemplate
can injected contentcontrol
. resolve view have few choices. can create datatemplateselector
map them or specify vm types on templates. in either case, make sure templates in scope @ contentcontrol
(in resources
in same file or parent, or merged standalone resourcedictionary
).
<datatemplate datatype="{x:type viewmodels:firstreportviewmodel}"> <views:firstreportviewcontrol/> </datatemplate>
Comments
Post a Comment