wpf - How to bind a property of a user control to the MainViewModel and bind its datacontext to its own viewmodel? -


a newbie binding question. have multiple usercontrols called mainview.xaml, like:

  <i:inkrichtextview richtext ="{binding lastnote}" ... /> 

where richtext dependency property in code-behind of inkrichtextview.xaml user control , lastnote property of mainviewmodel.

the mainviewmodel associated implicitly mainview way of datatemplate in app.xaml, like:

 <datatemplate datatype="{x:type vm:mainviewmodel}">         <v:mainview/>  </datatemplate> 

i set usercontrol inkrichtextview own viewmodel, property of being held in mainviewmodel, like:

<i:inkrichtextview richtext ="{binding lastnote}" datacontext="{binding ucfirstcontrol}" ... /> 

but when so, user control inkrichtextview loses context lastnote (or looses access dependency property in code-behind, or both??).

how can maintain binding mainviewmodel's property of lastnote , still provide separate datacontext user control?

(keep in mind usercontrol has dependency properties defined in code-behind).

thanks this.

give mainview name via x:name attribute. then, richtext property, can use binding like: richtext={binding elementname=mainviewname, path=datacontext.lastnote}

really, can use control has mainviewmodel datacontext in elementname property of binding. may easier since addressing controls generated datatemplate name can tricky.

here's example of general concept: note how textbox bound childname implicitly based on datacontext, while textbox next binds through parentcontrol (using elementname) name property on datacontext.

xaml:

<window x:class="wpfapplication3.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="mainwindow" height="350" width="250">     <stackpanel x:name="parentcontrol" orientation="vertical" datacontext="{binding}">         <textbox text="{binding name}"/>         <stackpanel datacontext="{binding childviewmodel}" margin="10">             <textbox text="{binding childname}"/>             <textbox text="{binding elementname=parentcontrol, path=datacontext.name}"/>         </stackpanel>     </stackpanel> </window> 

cs:

public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();         this.datacontext = new mainviewmodel();     }  }  public class propertynotifier : inotifypropertychanged {     public event propertychangedeventhandler propertychanged;      protected void onpropertychanged(string propname)     {         var propchanged = propertychanged;         if (propchanged != null)             propchanged(this, new propertychangedeventargs(propname));     } }  public class mainviewmodel : propertynotifier {     private string _name = "mainviewmodelproperty";     private childviewmodel _childviewmodel = new childviewmodel();      public string name     {         { return _name; }         set         {             _name = value;             onpropertychanged("name");         }     }      public childviewmodel childviewmodel     {         { return _childviewmodel; }         set         {             _childviewmodel = value;             onpropertychanged("childviewmodel");         }     } }  public class childviewmodel : propertynotifier {     private string _childname = "childviewmodelproperty";      public string childname     {         { return _childname; }         set         {             _childname = value;             onpropertychanged("childname");         }     } } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -