In my C# XAML Windows 8.1 app, how can I iterate over my ListView? -
i'm new windows 8.1 development, xaml, , c#, if question rudimentary, please forgive me.
i've got <page>
in app contains <listview>
, so:
<listview itemssource="{binding mode=twoway}" x:name="listview_statistical"> <listview.itemtemplate> <datatemplate> <stackpanel orientation="horizontal"> <textblock style="{staticresource subheadertextblockstyle}" width="100" margin="10,20"> <run text="x/y " /> <!--<run text="{binding source={staticresource thispage}, path=i}" />--> </textblock> <textbox horizontalalignment="left" text="{binding xval}" placeholdertext="x" inputscope="number" fontsize="28" width="100" margin="0,10,10,10" /> <textbox horizontalalignment="left" text="{binding yval}" placeholdertext="y" inputscope="number" fontsize="28" width="100" margin="0,10,10,10" /> </stackpanel> </datatemplate> </listview.itemtemplate> </listview>
in code behind, set datacontext so:
listview_statistical.datacontext = this.statisticalpoints;
this.statisticalpoints
defined such:
public observablecollection<statisticalpoint> statisticalpoints { { return (observablecollection<statisticalpoint>)getvalue(statisticalpointsproperty); } set { setvalue(statisticalpointsproperty, value); notifypropertychanged("statisticalpoints"); } } // using dependencyproperty backing store statisticalpoints. enables animation, styling, binding, etc... public static readonly dependencyproperty statisticalpointsproperty = dependencyproperty.register("statisticalpoints", typeof(observablecollection<statisticalpoint>), typeof(entercalc), new propertymetadata(0));
i'm not sure if making dependencyproperty necessary, or if making follow inotifypropertychanged necessary, don't seem hurt.
anyway, in constructor, add bunch of stuff statisticalpoints:
this.statisticalpoints = new observablecollection<statisticalpoint>(); this.statisticalpoints.add(new statisticalpoint() { xval = 1.0, yval = 2.0 }); this.statisticalpoints.add(new statisticalpoint() { xval = 33.0, yval = 44.0 }); this.statisticalpoints.add(new statisticalpoint() { xval = 555.0, yval = 666.0 }); this.statisticalpoints.add(new statisticalpoint() { xval = 0.7, yval = 0.8 });
and when load page, indeed see 5 rows in listview
, populated defined in initialization of this.statisticalpoints
.
the part i'm having trouble this:
i change first value in first <textbox>
in listview
, hit save button... listview.items doesn't have change reflected, , can't figure out how @ <textbox>
itself.
what want have bunch of statistical points modifiable user , able save changes. that, feel need read value in <textbox>
es, can't figure out how that.
alternatively, if 'right way' keep data in this.statisticalpoints
up-to-date when changes made in <textbox>
es, thought twoway
binding mode it, neither listview.items
nor this.statisticalpoints
changed when make changes in <textbox>
.
i do not have event handlers set in <textbox>
elements, can see, need them, or missing obvious?
thanks in advance can give me!
to solve initial problem, make binding each text box mode="twoway"
. reasons beyond comprehension, mode default oneway
on pretty in windows store apps.
making itemssource
binding 2 way next nothing, ui isn't changing collection itself (by changing, mean replacing). iterate on collection, iterate on this.statisticalpoints
, have current data.
now, have ton of other misconceptions try , run through them:
- you never showed save button, bindings either update source or don't. save button used persist changes view model model.
- speaking of view models, don't appear have one. shouldn't directly setting data context of controls, , shouldn't have in code-behind. create proper view model object page, , bind
itemssource
public property of view model. - notifypropertychanged on collection usually unnecessary unless replacing collection in code.
- having won't hurt though, except, setter of backing property of dependencyproperty (dp) never called framework, putting there weird
- and don't need dp @ all. dps there parent control can bind data special user control. until using user controls, , understand how dps work, shouldn't need use them.
Comments
Post a Comment