c# - Property that changes in constructor or as property -


i working c# using mvvm pattern. have 2 wpf windows, each view model. need pass property of main view model 'child' view model. @ minute, setting private variable equal new view model in main view model's constructor, , in doing passing property in constructor of child view model.

however, there dependency property linked property used binding selected item in combobox. therefore, change after child view model initialized, passing property in constructor, change not made in child view model.

so, there anyway me pass property constructor , have change in child view model when in main view model? or have create property in child view model updated everytime property in main view model set?

hope makes sense.

edit inside main view model, declare following:

public readonly dependencyproperty currentdatabasemanagedproperty = dependencyproperty.register("currentdatabasemanaged", typeof(databaseinfo), typeof(mainviewmodel)); public databaseinfo currentdatabasemanaged {     { return (databaseinfo)getvalue(currentdatabasemanagedproperty); }     set { setvalue(currentdatabasemanagedproperty, value); } }  public databaseinfo currentdatabasemanagedselection {     { return currentdatabasemanaged; }     set {         if (currentdatabasemanaged != null &&             (string.equals(value.name, currentdatabasemanaged.name, stringcomparison.ordinalignorecase))) return;              currentdatabasemanaged = (value.isusable ? value : dbm.readdatabase(value.filename));         } } 

where currentdatabasemanagedselection selecteditem of combobox. in constructor of main view model, have following:

_databasevm = new childviewmodel(currentdatabasemanaged); 

and constructor of childviewmodel looks this:

public childviewmodel( databaseinfo selecteddatabase)   {     if (selecteddatabase != null)          _selecteddatabase = selecteddatabase;     }   } 

basically _selecteddatabase updated whenever currentdatabasemanagedselection is.

it looks want bind currentdatabasemanagedselection property. simplest way simulate add setter of property:

_databasevm._selecteddatabase = value; 

to actual bindings, you'd need to

  1. make childviewmodel._selecteddatabase dependency property,
  2. make mainviewmodel implement inotifypropertychanged, and
  3. call propertychanged event in setter of currentdatabasemanagedselection.
  4. make childviewmodel extend dependencyobject
  5. instead of setting property, set binding, e.g.

    bindingoperations.setbinding(this, _selecteddatabaseproperty,   new binding("currentdatabasemanagedselection") { source = mainviewmodel }); 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -