我有两个ListBox
esSource
和Destination
. 目的地ListBox
已由Items
用户选择。
如何对目的地中的项目求和ListBox
?
目的地ListBox
有两种数据类型String
(描述)和decimal
(成本)。我只想总结成本。这是 XAML 代码
<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>
这是我试图总结成本的代码:
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;
他们选择Items
他/她需要的用户,我的挑战是获取所选项目的总数(已移至ListBox
目的地)