Tuesday, 15 June 2010

c# - Finding every checkbox control on an xaml page -



c# - Finding every checkbox control on an xaml page -

i'm trying programatically find checkboxes can see if checked. code below xaml looks , there checkbox created every item in list. know how can in code?

<scrollviewer grid.columnspan="5" grid.row="3" height="350" name="scrollviewer" > <itemscontrol name="lsttop10picks"> <itemscontrol.itemtemplate> <datatemplate> <grid margin="6" name="gridtop11stocks"> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="*" /> <columndefinition width="2*" /> <columndefinition width="2*" /> <columndefinition width="2*" /> </grid.columndefinitions> <checkbox style="{staticresource checkstyle}" grid.column="0" grid.row="3"> <checkbox.rendertransform> <scaletransform scalex="0.5" scaley="0.5" /> </checkbox.rendertransform> </checkbox> <textblock style="{staticresource numberstyle}" grid.column="1" grid.row="3" text="{binding id}" /> <textblock style="{staticresource summarystyle}" grid.column="2" grid.row="3" text="{binding symbol}" horizontalalignment="left" /> <textblock style="{staticresource summarystyle}" grid.column="3" grid.row="3" text="{binding market}" /> <textblock style="{staticresource summarystyle}" grid.column="4" grid.row="3" text="{binding return}" /> </grid> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </scrollviewer> var stocks = doc.element(ns + "arrayofstockrating").elements(ns + "stockrating") .select(n => new { id = count += 1, symbol = n.element(ns + "symbol").value, market = n.element(ns + "market").value, homecoming = n.element(ns + "shortrating").value }) .tolist();

lsttop10picks.itemssource = stocks;

a improve approach add together property in model store it's state (checked/unchecked) :

public class mymodel { ..... ..... public bool? ischecked { get; set; } }

then bind checkbox above mentioned property :

<checkbox ischecked="{binding ischecked}" style="{staticresource checkstyle}" grid.column="0" grid.row="3"> <checkbox.rendertransform> <scaletransform scalex="0.5" scaley="0.5" /> </checkbox.rendertransform> </checkbox>

that way don't have code messy trying find checkbox xaml, instead can iterate through model , check it's ischecked property (or improve using linq).

update :

turned out you're using anonymous type here, don't need class definition or partial class. alter linq select() part provide ischecked property default value set false, illustration :

.select(n => new { id = count += 1, symbol = n.element(ns + "symbol").value, market = n.element(ns + "market").value, homecoming = n.element(ns + "shortrating").value, ischecked = (bool?)false })

then can iterate through model later :

foreach(dynamic n in (ilist)lsttop10picks.itemssource) { bool? ischecked = n.ischecked; //do ischecked }

c# xaml checkbox windows-phone

No comments:

Post a Comment