c# - How to bind a list in store apps -
i have problem binding list in store apps.
public class category { public category(int id, string name) { this.id = id; this.name = name; } public int id { get; set; } public string name { get; set; } }
i created colllecionviewsource , gridview
<collectionviewsource x:name="categoriesviewsource" issourcegrouped="true"/> <gridview itemssource="{binding source={staticresource categoriesviewsource}}" > <gridview.itemtemplate> <datatemplate> <stackpanel> <textblock text="{binding name}"></textblock> <textblock text="{binding id}"></textblock> </stackpanel> </datatemplate> </gridview.itemtemplate> </gridview>
in constructor of page add list of category collectionviewsource
public hubpage() { this.initializecomponent(); this.navigationhelper = new navigationhelper(this); this.navigationhelper.loadstate += navigationhelper_loadstate; list<category> test = new list<category>(); test.add(new category(1, "two")); categoriesviewsource.source = test; }
but doesn't work... wrong?
i think main problem use of x:name attribute instead of x:key supposed used - what's difference between x:key , x:name in wpf? (it wpf xaml , controls same win store apps)
<collectionviewsource x:key="categoriesviewsource" issourcegrouped="true"/>
edit
well, bit confused, because resource key , other docs pertained wpf xaml x:key
should used resources, while this winrt xaml example on msdn shows equivalent use of x:name
but msdn says:
in general, x:name should not applied in situations use x:key. xaml implementations specific existing frameworks have introduced substitution concepts between x:key , x:name, not recommended practice.
so, not sure source of problems.
edit:
try to
hubpage: page, inotifypropertychanged { private void onpropertychanged(string propname) { if (this.propertychanged != null) this.propertychanged(this, new propertychangedeventargs(propname)); } ... public hubpage() { this.initializecomponent(); this.navigationhelper = new navigationhelper(this); this.navigationhelper.loadstate += navigationhelper_loadstate; list<category> test = new list<category>(); test.add(new category(1, "two")); this.categories = new observablecollection<category>(test); } private observablecollection<category> _categories; observablecollection<category> categories { {return this._categories;} private set { this._categories = value; this.onpropertychanged("categories"); } } } <page datacontext="{binding relativesource={relativesource self}}" ... <gridview itemssource="{binding categories}" > <gridview.itemtemplate> <datatemplate> <stackpanel> <textblock text="{binding name}"></textblock> <textblock text="{binding id}"></textblock> </stackpanel> </datatemplate> </gridview.itemtemplate> </gridview>
page main element in xaml.
Comments
Post a Comment