c# - Dynamically Create Controls in MVVM -


i pretty new wpf. trying create controls dynamically in mvvm controls not rendered on view. want number of label , textbox created on view.

below code:

model code

public class mymodel {     public string keyname     {         get;         set;     }      public string keyvalue     {         get;         set;     } } 

modelview code

public class myviewmodel {     private observablecollection<mymodel> propertieslist = new observablecollection<mymodel>();     public customwriterviewmodel()     {         getmyproperties()      }       public observablecollection<mymodel> properties     {         { return propertieslist; }     }      private void getmyproperties()     {         mymodel m = new mymodel();         m.keyname = "test key";         m.keyvalue = "test value";         mymodel.add(m);      } } 

view code(which user control)

<grid>     <itemscontrol itemssource="{binding properties}">         <itemscontrol.itemtemplate>             <datatemplate datatype="{x:type cw:mymodel}">                 <stackpanel orientation="horizontal">                     <label margin="10" content="{binding properties.keyname}"></label>                     <textbox margin="10" text="{binding properties.keyvalue}" width="250"></textbox>                 </stackpanel>             </datatemplate>         </itemscontrol.itemtemplate>     </itemscontrol> </grid> 

when view renders, can see empty textbox. cannot understand wrong..?

as per comment:

the datatemplate receives individual item datacontext, therefore need include item level property names within binding paths like:

<datatemplate datatype="{x:type cw:mymodel}">     <stackpanel orientation="horizontal">          <label margin="10" content="{binding keyname}"></label>          <textbox margin="10" text="{binding keyvalue}" width="250"></textbox>     </stackpanel> </datatemplate> 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -