c# - How Do I Sum Items in the List box? -
i have 2 listbox
es, source
, destination
. destination listbox
has items
selected user.
how sum items in destination listbox
?
the destination listbox
has 2 data types string
(description) , decimal
(cost). want sum cost. here xaml code
<listbox height="237" horizontalalignment="left" margin="44,191,0,0" name="lstdestination" verticalalignment="top" width="264" grid.columnspan="2" itemssource="{binding source={staticresource myitemlist}}"> <listbox.itemtemplate> <datatemplate> <stackpanel orientation="vertical"> <dockpanel > <textblock fontweight="bold" text="item:" dockpanel.dock="left" margin="5,0,10,0"/> <textblock text="{binding path=resource}" foreground="green" fontweight="bold" /> <textblock fontweight="bold" text="cost:" dockpanel.dock="left" margin="5,0,10,0"/> <textblock text="{binding path=cost}" fontweight="bold" /> </dockpanel> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox>
this code tried sum cost:
private decimal sumdlstbox() { decimal totalcost; foreach (var in lstdestination.selecteditems) { totalcost+=i.cost; //error thrown here } return totalcost; } //data source list box source var items = r in dc.iresources select r; lstsource.itemssource = items;
the user them selects items
s/he needs, challenge total of selected items(which have been moved listbox
destination)
to operate on bound data, should cast list of datatype you're working with. eg.
private decimal sumdlstbox() { return lstdestination.selecteditems.cast<foo>().sum(f => f.cost); }
where foo
datatype of list bound control.
Comments
Post a Comment