c# - Communicating with ViewModel from MainView -


i new mvvm , still trying grasp on let me know if i'm setting wrong. have usercontrol listview in it. populate listview data viewmodel add control mainview. on mainview have button want use add item listview. here have:

model

public class item {     public string name { get; set; }      public item(string name)     {         name = name;     } } 

viewmodel

public class viewmodel : inotifypropertychanged {      #region inotifypropertychanged members      public event propertychangedeventhandler propertychanged;      private void onpropertychanged(string propertyname)     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     }     #endregion      private observablecollection<item> _itemcollection;      public viewmodel()     {         itemcollection = new observablecollection<item>()         {             new item("one"),             new item("two"),             new item("three"),             new item("four"),             new item("five"),             new item("six"),             new item("seven")         };     }      public observablecollection<item> itemcollection     {                 {             return _itemcollection;         }         set         {             _itemcollection = value;             onpropertychanged("itemcollection");         }     } } 

view (xaml)

<usercontrol.resources>     <datatemplate x:key="itemtemplate">         <stackpanel orientation="vertical">             <label content="{binding name}" />         </stackpanel>     </datatemplate> </usercontrol.resources>  <usercontrol.datacontext>     <local:viewmodel /> </usercontrol.datacontext>  <grid>     <listview itemtemplate="{staticresource itemtemplate}" itemssource="{binding itemcollection}">      </listview> </grid> 

mainwindow

public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();         this.maincontentcontrol.content = new listcontrol();     }      private void button_add(object sender, routedeventargs e)     {      } } 

mainwindow (xaml)

<grid>     <dockpanel>         <stackpanel dockpanel.dock="top" orientation="horizontal">             <button width="100" height="30" content="add" click="button_add" />          </stackpanel>         <contentcontrol x:name="maincontentcontrol" />     </dockpanel> </grid> 

now, understand, should able item itemcollection , updated in view. how do button_add event?

again, if i'm doing wrong let me know , point me in right direction. thanks

you should not interact directly controls.

what need define command (a class implements icommand-interface) , define command on viewmodel.

then bind button's command property property of viewmodel. in viewmodel can execute command , add item directly list (and listview updated through automatic databinding).

this link should provide more information:

http://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx#sec11


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -