c# - Change data from UI with data binding -
my understanding data binding in windows 10 programming gui can reflects data change , can manipulate data. wrong here?
i have list of customized data defined in app.
public static observablecollection<screen> screens;
the structure of screen like
public class screen : inotifypropertychanged { private string name; public string name { { return this.name; } set { if (this.name != value) { this.name = value; this.notifypropertychange("name") } } } public bool enabled { /* same name*/ } public event propertychangedeventhandler propertychanged; public void notifypropertychanged(string propname) { if (this.propertychanged != null) { this.propertychange(this, new propertychangeeventargs(propname)); } } }
in 1 of page, let's page1, define listbox in xaml
<listbox name="screenlistbox"> <listbox.itemtemplate> <datatemplate> <grid.columndefinitions> <columndefinition width="100"> <columndefinition width="*"> </grid.columndefinitions> <checkbox ischecked="{binding enabled}"> <textbox grid.column="1" text="{binding name}"> <datatemplate> <listbox.itemtemplate> </listbox>
and bind listbox screens list in app
screenlistbox.itemssource = app.screens
in constructor of page1, instantiate 2 screens
app.screens = new observablecollection<screen>() { /*some initial list*/ };
there page, let's page2, click button navigate page1 page2, since screens in app , public static. in page2 have access them.
this.frame.navigate(typeof(page2), null);
the problem after make changes via gui on page1, doesn't reflect screens list in app. example if change name of first item in list on page1 set break point before page switching, can see screens list not changed. try use checkbox tapped handler , manually update isenable property. can't seem find checkbox's corresponding index in list.
any appreciated! thanks.
edit: make screen inherited inotifypropertychanged.
edit: use observablecollection instead of list
edit: can't figure out index of tapped checkbox in event handler
i think others' suggestions helpful. , @justin xl has pointed out point how can make reflect backend object/collection.
make binding twoway. there 2 scenarios:
if want reflect change properties name , enabled. make following change:
<checkbox ischecked="{binding enabled, mode=twoway}"/> <textbox grid.column="1" text="{binding name, mode=twoway}"/>
if want reflect collection add/delete actions, use customized binding itemssource property below:
binding sbinding = new binding(); sbinding.source = app.screens; sbinding.mode = bindingmode.twoway; screenlistbox.setbinding(itemscontrol.itemssourceproperty, sbinding);
i've tested scenarios on end, , works well.
Comments
Post a Comment