I have two ListBox
es, Source
and Destination
. The destination ListBox
has Items
selected by the user.
How do I sum items in a destination ListBox
?
The Destination ListBox
has two data types String
(Description) and decimal
(Cost).
I want to sum up only the Cost. Here is the 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 is the code I tried to sum up Cost:
private decimal SumDLstBox()
{
decimal totalCost;
foreach (var i in lstDestination.SelectedItems)
{
totalCost+=i.Cost; //Error is thrown here
}
return totalCost;
}
//Data Source to List Box Source
var items = from r in dc.IResources select r;
lstSource.ItemsSource = items;
The user them selects the Items
s/he needs, my challenge is to get the total of the selected items(Which have been moved to the ListBox
Destination)