c# - Binding to DependencyProperty not working -


i've created dp in following way:

 public static readonly dependencyproperty schoolsviewmodelproperty =         dependencyproperty.register("schoolsviewmodel", typeof(schoolsviewmodel), typeof(schoolchooser), new propertymetadata(default(schoolsviewmodel)));  public schoolsviewmodel schoolsviewmodel  {         {         return (schoolsviewmodel)getvalue(schoolsviewmodelproperty);     }      set     {        setvalue(schoolsviewmodelproperty, value);        this.onpropertychanged();     }  }  public event propertychangedeventhandler propertychanged;   [notifypropertychangedinvocator]  private void onpropertychanged([callermembername] string propertyname = null)  {      propertychangedeventhandler handler = propertychanged;      if (handler != null)      {          handler(this, new propertychangedeventargs(propertyname));      }  } 

next, i'm binding datacontext:

<common:layoutawarepage     ...     datacontext="{binding elementname=schoolchooserpage, path=schoolsviewmodel}"/> 

then, i'm setting property in costructor:

public schoolchooser() {     this.initializecomponent();      this.schoolsviewmodel = new schoolsviewmodel { schools = typeservice.resolve<ischoolcontext>().schools }; } 

infortunately, datacontext null. seems me, binding not working. i've no idea why, such way working in wpf.

when set datacontext directly like:

this.datacontext = this.schoolsviewmodel; 

everything fine. why? what's, althought i've implement inotfiyproperychanged, event properychanged null.

you don't need inotifypropertychanged dependencypropertys. maybe removing give better view on problem.

i wonder why view has property viewmodel. shouldn't viewmodel go datacontext? expect view , viewmodel independent instance application creating both objects , connecting them setting views datacontext viewmodel instance.

example:

var view = new view(); var viewmodel = new viewmodel();  view.datacontext = viewmodel;  view.show(); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -