c# - Binding a collection not working -
c# - Binding a collection not working -
i'm working on windows phone app has listbox itemtemplate. i'm trying bind observablecollection of viewmodel listbox nil beingness shown.
the xaml of page looks this:
... <phone:phoneapplicationpage.resources> <datatemplate x:key="myitemtemplate"> <grid height="auto" verticalalignment="top" width="auto"> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="*" /> </grid.rowdefinitions> <textblock text="{binding path=name}" style="{staticresource phonetextlargestyle}" grid.row="0"/> <textblock text="{binding path=description}" style="{staticresource phonetextsubtlestyle}" grid.row="1"/> </grid> </datatemplate> </phone:phoneapplicationpage.resources> ... <grid x:name="contentpanel" grid.row="1" margin="12,0,12,0"> <listbox x:name="mylistbox" itemssource="{binding path=myitems, mode=twoway}" margin="12, 0, 12, 0" itemtemplate="{staticresource myitemtemplate}"/> </grid> ...
and here's classes myitem , myviewmodel has observablecollection i'm trying bind to:
public class myitem { public string name { get; set; } public string description { get; set; } } public class myviewmodel { public readonly observablecollection<myitem> myitems = new observablecollection<myitem>(); }
i still have implement inotifypropertychanged interface.
and lastly, in constructor of page set datacontext instance of viewmodel:
public mainpage() { myviewmodel viewmodel = new myviewmodel(); viewmodel.myitems.add(new myitem() { name = "1st", description = "description" }); viewmodel.myitems.add(new myitem() { name = "2nd", description = "description" }); this.datacontext = viewmodel; }
that doesn't work. however, if set itemssource of listbox in code works fine!
mylistbox.itemssource = viewmodel;
so missing here?
your myitems
not property cannot bind it.
public class myviewmodel { private readonly observablecollection<myitem> _myitems = new observablecollection<myitem>(); public observablecollection<myitem> myitems { get{ homecoming _myitems; } } }
try this. allow create 1 way binding there no setter available.
c# xaml windows-phone-8 mvvm
Comments
Post a Comment