c# - WPF MvvM communication between view models in a single View -


this title might inappropriate question stay me  i’ll change if have better suggestion. first wpf application might missed key concept… did google, failed find correct approach.

i building wpf application using mvvm light , mui , got trouble item bindings, ie communication between view models. now, sure wouldn't have problem if used single view model page , user controls in it, think overdid on first try.

i have 1 main window in application , pages user controls. in each page have several other user controls , each 1 of them have own view model , own logic doing stuff in end depend on corresponding vm data grid. think of them poor man angular directives. each user control have data context defined so:

datacontext="{binding viewmodelname, source={staticresource locator}} 

layout looks this: wpf layout @ way, dg1 in vm1 master (customer) , uc3 , uc4 details (orders). if add new order customer, updated in dg1 without refreshing entire grid.

in vm1 data grid 1 selection changed firing commands set property values of depending user controls.

<i:interaction.triggers>             <i:eventtrigger eventname="selectionchanged">                 <mvvm:eventtocommand command="{binding errorworkflow.geterrorworkflowcommand, mode=oneway, source={staticresource locator}}" commandparameter="{binding selectederror.wf_ref}" />                 <mvvm:eventtocommand command="{binding errordetails.geterrorcasedetailscommand, mode=oneway, source={staticresource locator}}" commandparameter="{binding selectederror}" />              </i:eventtrigger>         </i:interaction.triggers> 

that part works ok, when change value of in depending vm3 both vm3 , vm1, vm1 property values not changed though call raisepropertychanged or setting property explicitly hand selectederror.status = “somethingnew”.

on other hand, if clear selection form data grid, depending view models stay bound (text boxes on them preserve value because referencing properties on own vm3).

all view models derive viewmodelbase mvvmlight , models observableobjects (i know should use poco, apparently have create each property on vm too). example:

        public const string selectederrorpropertyname = "selectederror";      private errorlog _selectederror;      public errorlog selectederror     {                 {             return _selectederror;         }         set         {             set(() => selectederror, ref _selectederror, value);          }     } 

i think messenger overkill considering size of application (only few pages one).

should change page use 1 view model , share them each user control or missing obvious here? if think missing key information in example please tell me , i’ll update.

thank in advance advice, cheers!

you should never change page 1 view model , messenger not overkill. mvvm light messenger built solve exact problem (communication between vms) having @ moment. should use it.

for further information messaging within mvvm light, jesse liberty of microsoft has a great tutorial on how make use of it.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -