windows phone 7 - ListPicker within LongListSelector not retaining value after scrolling -
i developing wp7 app contains longlistselector. within itemtemplate there listpicker. select value listpicker first item in longlistselector, select value listpicker 2nd item. if scroll down page , top again, value selected in listpicker 1st item reset (selectedindex=0).
i have put code in link , unlink events of longlistselector write output window , have found when 1st item unlinks (due scrolling down page), listpicker value selected link event fires (due scrolling page) 1st item value reset.
im using obserablecollection , implement inpc interface objects , viewmodel updates when listpicker selection changed.
how can ensure value in listpickers retaining during scrolling of longlistselector?
mainpage.xaml
<phone:phoneapplicationpage x:class="longlistselector.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:microsoft.phone.controls;assembly=microsoft.phone" xmlns:shell="clr-namespace:microsoft.phone.shell;assembly=microsoft.phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" d:designwidth="480" d:designheight="5000" fontfamily="{staticresource phonefontfamilynormal}" fontsize="{staticresource phonefontsizenormal}" foreground="{staticresource phoneforegroundbrush}" supportedorientations="portrait" orientation="portrait" shell:systemtray.isvisible="true" xmlns:toolkit="clr-namespace:microsoft.phone.controls;assembly=microsoft.phone.controls.toolkit"> <phone:phoneapplicationpage.resources> <datatemplate x:name="occurrenceitemtemplate"> <grid> <stackpanel orientation="vertical" margin="5,0,0,0" > <grid> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="*" /> </grid.columndefinitions> <textblock name="tbkeventdatetime" grid.row="0" grid.column="0" text="{binding itemnumber}" style="{staticresource phonetextsmallstyle}" /> <!--<textbox name="txteventdatetime" grid.row="1" grid.column="0" text="{binding result}" />--> <toolkit:listpicker name="lpkresult" margin="12,0,12,12" grid.row="1" grid.column="0" itemssource="{binding testitemresultlist, mode=onetime}" selectedindex="{binding result, mode=twoway}" cachemode="bitmapcache"> <toolkit:listpicker.itemtemplate> <datatemplate> <textblock text="{binding text}" /> </datatemplate> </toolkit:listpicker.itemtemplate> </toolkit:listpicker> </grid> </stackpanel> </grid> </datatemplate> </phone:phoneapplicationpage.resources> <!--layoutroot root grid page content placed--> <grid x:name="layoutroot" background="transparent"> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="*"/> </grid.rowdefinitions> <!--titlepanel contains name of application , page title--> <stackpanel x:name="titlepanel" grid.row="0" margin="12,17,0,28"> <textblock x:name="applicationtitle" text="my application" style="{staticresource phonetextnormalstyle}"/> <textblock x:name="pagetitle" text="page name" margin="9,-7,0,0" style="{staticresource phonetexttitle1style}"/> </stackpanel> <!--contentpanel - place additional content here--> <grid x:name="contentpanel" grid.row="1" margin="12,0,12,0" verticalalignment="stretch"> <toolkit:longlistselector x:name="lstoutstandingoccurrences" margin="0,12,0,0" padding="0,0,0,24" itemtemplate="{staticresource occurrenceitemtemplate}" itemssource="{binding testitemcollection, mode=twoway}" isflatlist="true" showlistheader="false" > </toolkit:longlistselector> </grid> </grid>
mainpage.xaml.cs
public partial class mainpage : phoneapplicationpage { private testitemviewmodel _vm; public testitemviewmodel viewmodel { { if (_vm == null) _vm = new testitemviewmodel(); return _vm; } private set { _vm = value; } } // constructor public mainpage() { initializecomponent(); this.lstoutstandingoccurrences.link += new eventhandler<linkunlinkeventargs>(lstoutstandingoccurrences_link); this.lstoutstandingoccurrences.unlink += new eventhandler<linkunlinkeventargs>(lstoutstandingoccurrences_unlink); this.datacontext = this.viewmodel; } void lstoutstandingoccurrences_link(object sender, linkunlinkeventargs e) { var item = e.contentpresenter.content testitem; debug.writeline("link event itemnumber {0} = {1}", item.itemnumber, item.result); } void lstoutstandingoccurrences_unlink(object sender, linkunlinkeventargs e) { var item = e.contentpresenter.content testitem; debug.writeline("unlink event itemnumber {0} = {1}", item.itemnumber, item.result); } }
testitemviewmodel.cs
public class testitemviewmodel : baseinpc { public observablecollection<testitem> testitemcollection { get; private set; } // constructor public testitemviewmodel() { this.testitemcollection = new observablecollection<testitem>(); createtestdata(20); } public void createtestdata(int totalitems) { //create test data long list selector. (int = 1; <= totalitems; i++) { this.testitemcollection.add(new testitem(i, 0)); } } }
models
public class listhelperlistitem { public int value { get; set; } public string text { get; set; } } public class testitem : baseinpc { public testitem(int itemnumber, int result) { this.itemnumber = itemnumber; this.result = result; } public int itemnumber { get; set; } private int _result; public int result { { return _result; } set { //if statement debugging purposes only. if (this.itemnumber == 1) { _result = value; } _result = value; raisepropertychanged("result"); } } private list<listhelperlistitem> _testitemresultlist; public list<listhelperlistitem> testitemresultlist { { _testitemresultlist = new list<listhelperlistitem>(); _testitemresultlist.add(new listhelperlistitem { value = 0, text = " " }); _testitemresultlist.add(new listhelperlistitem { value = 1, text = "yes" }); _testitemresultlist.add(new listhelperlistitem { value = 2, text = "no" }); _testitemresultlist.add(new listhelperlistitem { value = 3, text = "ignore" }); return _testitemresultlist; } } }
thanks help!
Comments
Post a Comment